Money gem converting variable of currency code to lower case

493 views Asked by At

I am using the money gem in rails to perform some currency conversion.

I'd like to dynamically set the conversion rate so that I can use it in a script.

currency_code = ":SEK"

conversion_rate = @bank.get_rate(:USD, currency_code).to_f

I get this error:

Money::Currency::UnknownCurrency: Unknown currency ':sek'

Which means it's converting the variable to lower case. If I explicitly put in :SEK I don't have any issues.

I've even tried playing around with this:

cb = "SEK"

conversion_rate = @bank.get_rate(:USD, ":#{cb}").to_f

And

cc = ":SEK"

conversion_rate = @bank.get_rate(:USD, cc.upcase).to_f

However I get the same error.

Any ideas?

1

There are 1 answers

0
Jeff Price On BEST ANSWER

bnussey,

I looks like you are passing in your currency as a string instead of a symbol. Try this instead:

currency_code = :SEK

If you need to store a string in the database, Ruby can easily convert it to a symbol.

currency = "SEK"
currency_code = currency.to_sym
=> :SEK