Skip to content

Instantly share code, notes, and snippets.

@zdennis
Created November 8, 2008 02:35
Show Gist options
  • Save zdennis/23012 to your computer and use it in GitHub Desktop.
Save zdennis/23012 to your computer and use it in GitHub Desktop.
class Expense < ActiveRecord::Base
composed_of :amount, :class_name => "Money", :mapping => [ %w(amount_in_cents cents)], :converter => lambda { |amount| amount.to_money }
end
class CreateExpenses < ActiveRecord::Migration
def self.up
create_table :expenses do |t|
t.integer :amount_in_cents
t.timestamps
end
end
def self.down
drop_table :expenses
end
end
# even though amount_in_cents is stored in the database, you access the the amount
# by the amount method.
expense = Expense.create! :amount => "$5.00"
expense.amount
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment