Ruby Money vs nil — aka undefined method `round’ for nil:NilClass

When using composed_of with Money you get this nice error when trying to get the money object while the cents column is nil.
Additionally does not work, neither do strings like[:cents]).

Overwrite the initializer to also accept nil and strings

# config/initializers/money.rb
require 'money'
class Money
  def initialize_with_default_currency(*args)
    args[0] = args[0].to_i if args[0].is_a?(String) or args[0].blank?
    args[1] ||= Money.default_currency
  alias_method_chain :initialize, :default_currency

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s