6

我希望能够使用模型父级设置的货币动态设置模型上的货币。

像这样:

class Event < ActiveRecord::Base
  belongs_to :edition
  monetize :price_cents, :with_currency => proc { |event| event.edition.currency }

event.edition.currency 从模型的父级返回一个符号......例如:gbp

但它不起作用。默认约定是:

monetize :bonus_cents, :with_currency => :gbp

哪个效果很好......有什么想法吗?

https://github.com/RubyMoney/money-rails

4

1 回答 1

3

尝试这个:

class Event < ActiveRecord::Base
  belongs_to :edition
  monetize :price_cents

  def currency_for_price
    Money::Currency.find(edition.currency)
  end
end

我没有彻底测试它,但它似乎工作。

2.0.0-p195 :012 > Event.new(
                      edition: Edition.new(currency: :gbp),
                      price: 123
                  ).price
 => #<Money fractional:12300 currency:GBP>
2.0.0-p195 :013 > Event.new(
                      edition: Edition.new(currency: :usd),
                      price: 456
                  ).price
 => #<Money fractional:45600 currency:USD>
于 2013-07-18T10:59:41.937 回答