Skip to content

Why is the currency ignored on assignment? #87

Closed
skorfmann opened this Issue Mar 3, 2013 · 7 comments

6 participants

@skorfmann

In the following example, the currency of the assigned money object is just silently ignored:

class Item < ActiveRecord::Base  
  monetize :price_cents
end

i = Item.new
i.price = Money.new(500, 'AUD') 
i.price # => <Money fractional:500 currency:USD>

That's totally confusing and not the way I've expected it to work. What's the idea behind this behaviour?

@geehsien

Do you have instance-level currency? If so, what is the name of your currency attribute?

If it's anything other than currency, you may need the :with_model_currency option.

If your currency attribute is price_currency, try:

monetize :price_cents, :with_model_currency => :price_currency
@swistak
swistak commented Apr 9, 2013

I had the same issue.
Setting :with_model_currency works, but this is not expected behaviour. According to documentation this should not be needed as migration helper created price_currency.

Also assigning values like "400 GBP" which are perfectly fine with Money.parse is ignored.

@swistak
swistak commented Apr 9, 2013

I've dug into code and looks like money-rails is hardcoding one currency column per row instead of amount / currency pairs that money helper generates. As a result it's for example impossible to have 2 columns with different currencies.

Will work on fix for that in spare time. .monetize needs serious refactoring.

@aboutlo
aboutlo commented May 29, 2013

I'm stuck on a similar problem. I have the column price where I fill prices in GBP, EUR and USD currencies. Unfortunately all my price are not based on currency stored into the model but on default currency.
I'm using a workaround like Money.new(myproduct.price_cents, myproduct.price_currency) to regenerate the right money object.

@alup
RubyMoney member
alup commented May 29, 2013

@aboutlo Take a look at this https://github.com/RubyMoney/money-rails#instance-currencies
The "instance currency column" should be named after currency, else you may need the :with_model_currency option as @geehsien mentioned.

@aboutlo
aboutlo commented May 30, 2013

thank you, it works. I missed the @geehsien tip. IMHO should be the default behavior or at least added to the https://github.com/RubyMoney/money-rails#instance-currencies page.

@alup alup closed this Jun 12, 2013
@glacier3

Cool ! @geehsien

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.