RubyMoney/money-rails

Assocation .build clears amount if association called during setup

nitsujri opened this issue · 2 comments

TL;DR

If a model association is called during .build for setting with_model_currency, the column_cents gets reset to 0.

Example

https://github.com/nitsujri/money-build-issue

To run it, load the rails console and run User.run_example.

Details

It will run through showing .build works fine if we don't call the assocation, user.currency, during with_model_currency, but then if we do call an association, amount_cents becomes zero.

The problem line is this. The only difference is a random user.currency that has no bearing on the final output.

After build, setting amount works just fine as we all would expect.

Closing

This specifically caught us during a accepts_nested_attributes_for form. I left all the deprecations in, so the output is a bit messy.

I can also get a similar situation with u=User.create; Purchase.new(user: u, amount: '222') so it's build/new initializer? I'm just guessing at this point though.

Thank you!

Thanks for this fantastic set of gems!