RubyMoney/money-rails

Wrong currency is ignored when not using `_currency` attribute

Closed this issue · 2 comments

When a model does not have a currency attribute, monetize does not check whether the value passed to amount= uses the correct currency.

e.g.

class Payment < ApplicationRecord
  monetize :amount_cents, with_currency: :usd
end

p = Payment.new
p.amount = Money.from_cents(100, :eur)
puts p.valid? # true
puts p.amount.inspect # #<Money fractional:100.0 currency:USD>

The currency is silently converted from EUR to USD with no error.

Ah, it turns out there is an option MoneyRails.raise_error_on_money_parsing that needs to be set to true, but is undocumented.

The generated setter by monetize still silently ignore a currency if not found and use the default value. And it won't raise any error even if setting MoneyRails.raise_error_on_money_parsing.