Skip to content

Instantly share code, notes, and snippets.

@nilakanta
Created September 1, 2011 03:17
Show Gist options
  • Save nilakanta/1185352 to your computer and use it in GitHub Desktop.
Save nilakanta/1185352 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