Skip to content

Instantly share code, notes, and snippets.

@zefer
Created February 23, 2011 14:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zefer/840500 to your computer and use it in GitHub Desktop.
Save zefer/840500 to your computer and use it in GitHub Desktop.
Money represented with the Money gem, persistence using Mongoid
class Product
include Mongoid::Document
include Mongoid::Timestamps
field :description, :type => String
field :price_pence, :type => Integer, :default => 0
field :currency_iso, :type => String, :default => Money.default_currency.iso_code
validates_presence_of :description
validates_numericality_of :price_pence
# Virtual price / currency attributes
def price
Money.new(self.price_pence, currency)
end
def price=(price)
self.price_pence = price.cents
end
def currency
Money::Currency.new(currency_iso)
end
end
@jattoabdul
Copy link

How do you create a new instance of this product model?

Do you pass price or price_pence as attribute?
Product.new(price: 1000, currency_iso: 'CAD')
OR
Product.new(price_pence: 1000, currency_iso: 'CAD')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment