I'm using the Ruby Money gem (For rails) for my app and i want to save the used currency rate for every order.

My base currency is USD and i give my users the option to pay in EUR, on a order save i want to record the used currency conversion rate.

I just can't find a method to get the used rate from this lib, anybody knows how to do this?

I'm also looking for best practice on this, for now i'm planning on saving the prices in order_lines in the users currency and save the used currency per line. As my original prices are in dollars i'm also saving the price in dollar per order line as reference.

Thanks in advance!

有帮助吗?

解决方案

For this example bank:

# config/initializers/money.rb
dev_bank = Money::Bank::VariableExchange.new
dev_bank.add_rate("EUR", "USD", 1.35)
dev_bank.add_rate("USD", "EUR", 1/1.35)
Money.default_bank = dev_bank

you can do this:

# somewhere else in your code
Money.default_bank.get_rate('EUR', 'USD')

See more info in the documentation

其他提示

It looks like you're supposed to set up exchange rates in your configuration code using the exchange bank object or the money.rb initializer, in which case you already have access to the exchange rate in your code.

If for some reason you have access only to the input and output of the exchange conversion, you should be able to calculate the exchange rate yourself.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top