Skip to content

Instantly share code, notes, and snippets.

@dsapala
Forked from nilakanta/my_app_money.rb
Created January 29, 2012 20:24
Show Gist options
  • Save dsapala/1700476 to your computer and use it in GitHub Desktop.
Save dsapala/1700476 to your computer and use it in GitHub Desktop.
Use Money gem with mongoid (works with mongoid 2.1.0 and higher)
module MyApp
class Money
include Mongoid::Fields::Serializable
def cast_on_read?; true; end
def deserialize(object)
return nil if object.blank?
::Money.new(object[:cents] || object["cents"], object[:currency] || object["currency"])
end
def serialize(object)
return nil if object.blank?
{:cents => object.cents, :currency => object.currency_as_string}
end
end
end
class MyModel
include Mongoid::Document
field :price, type: MyApp::Money
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment