Reputation: 5151
How should I work with Money with MongoID? Should I configure it as BigDecimal? And at rails level? For ActiveRecord we have something called Money, but AFAIK it just supports AR
Upvotes: 5
Views: 4452
Reputation: 9791
If you like the money gem you can store it as a Money type.
An example: https://gist.github.com/michaelkoper/5007636
It stores the money as an array [cents, currency]
class Product
include Mongoid::Document
field :price, type: Money
end
product = Product.new(:price => Money.new(1000, 'EUR'))
product.price.format
# => "€10.00"
Upvotes: 2
Reputation: 171
I recommend you try money-rails as an alternative. https://github.com/RubyMoney/money-rails It is pretty well maintained and works with mongoid too!
Upvotes: 0
Reputation: 19873
I ran into this also. Unfortunately BigDecimal stores in Mongodb as a string, so it won't let you sum, sort, etc on it like a float or int.
Integer seem to be the way to go storing the value in cents, possibly using the Money gem to abstract it a bit: https://github.com/RubyMoney/money
Mongo stores the int using 64 bits on most modern machines I think so there is not much risk of needing a larger amount even in cents. It looks like you can store between −9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 cents, so take off two decimal places to get your min/max value in dollars.
http://en.wikipedia.org/wiki/Integer_(computer_science)
Upvotes: 8