forked from RubyMoney/money
-
Notifications
You must be signed in to change notification settings - Fork 1
Ruby on Rails (Money.gem 2.3.* an later)
semmons99 edited this page Sep 12, 2010
·
4 revisions
Use the #composed_of
helper to let Active Record deal with embedding the money object in your models. The following example requires a cents
and a currency
field.
composed_of :price,
:class_name => "Money",
:mapping => [%w(cents cents), %w(currency currency_as_string)],
:constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) }
Also, because the currency
attribute is not a simple String
but a Money::Currency
object, you need a small tweak to make it work.
Extend/hack the Money
class with two additional methods:
# You can put this file in the config/initializers/ folder class Money def currency_as_string currency.to_s end
def currency_as_string=(value) # WARNING: this method might cause data inconsistency. # See http://github.com/FooBarWidget/money/issues/4#issue/4/comment/224930 # currency = Currency.wrap(value) end end