使用lambda或proc和rails-money

ere*_*ere 6 lambda ruby-on-rails proc ruby-2.0 ruby-on-rails-4

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

像这样:

class Event < ActiveRecord::Base
  belongs_to :edition
  monetize :price_cents, :with_currency => proc { |event| event.edition.currency }
Run Code Online (Sandbox Code Playgroud)

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

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

monetize :bonus_cents, :with_currency => :gbp
Run Code Online (Sandbox Code Playgroud)

哪个工作正常......任何想法?

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

DNN*_*NNX 3

尝试这个:

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

  def currency_for_price
    Money::Currency.find(edition.currency)
  end
end
Run Code Online (Sandbox Code Playgroud)

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

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>
Run Code Online (Sandbox Code Playgroud)