Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Ruby on Rails (Money.gem 3.0.3 and later)

jcfischer edited this page · 14 revisions

Define accessor methods to let Active Record deal with embedding the money object in your models. The following example assumes a `price_cents` and a `price_currency` field.

def price
  Money.new price_cents, price_currency
end

def price=(value)
  value = Money.parse(value) if value.instance_of? String  # otherwise assume, that value is a Money object

  write_attribute :price_cents,    value.cents
  write_attribute :price_currency, value.currency_as_string
end

With the example above, your migration should include the following lines:

...
t.integer  :price_cents,    :default => 0, :null => false
t.string   :price_currency, :null => false
...
Something went wrong with that request. Please try again.