Optional attributes sets shared currency to nil #159

assembler opened this Issue Jan 24, 2014 · 5 comments

3 participants


I have a model which has 2 monetizable attributes: amount and fee.
amount is required, and fee is optional (allow_nil: true)
Both share same currency defined like this:

monetize :amount_cents, model_currency: :currency, numericality: { greater_than: 0 }
monetize :fee_cents, model_currency: :currency, allow_nil: true

However, when form is submitted, it realizes that fee is nil, it updates shared currency to nil. (https://github.com/RubyMoney/money-rails/blob/master/lib/money-rails/active_record/monetizable.rb#L156)

That results in instance currency being nil, where it should remain unchanged. I have solved the problem by setting the model_currency: :readonly_currency and not allowing it to be changed. Is there any way around it?

RubyMoney member

@RubyMoney, anyone have ideas? I've always just used multiple currency columns. Feel free to create a patch if it makes sense.

RubyMoney member

Why dont you use a "readonly" currency strategy like the "Model" or "Attribute" currency? Instance currencies are used to keep per row different currencies.

The problem here is that you are using two different ways to handle the currency value allow_nil: true and "not null" for the same currency instance. A cleaner solution would be to map each amount column to a separate currency one like @semmons99 mentioned. So, the Money values will be discreet.

In any case, it would also be nice to support cases that row level currency is shareable among multiple amount columns. But, this means that the currency should be updated in a separate manner. Money fields should not be able to touch the currency value, only the amount value!

I am leaving this issue open as it addresses a new feature.


Thanks for the answers. I migrated from activerecord, to form object + poro + repository (with datamapper). Storing amount to cents and loading and converting cents to money objects from database is done on datamapping layer.

RubyMoney member

Do you mean that you implemented a custom data mapping solution or are you using the DataMapper as an ORM? By the way, there is no glue code for money-rails and the latter, so if you are interested to provide a patch it would be great!


i've implemented custom data mapping solution. i don't really like the datamapper 1.x :)

@semmons99 semmons99 closed this Apr 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment