Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data type for currency using Mongoid

I guess floats are not ideal for currency. Mongoid supports Float and BigInteger. What's the best approach for storing and working with currency values?

like image 436
timstepp Avatar asked Nov 04 '10 21:11

timstepp


Video Answer


2 Answers

You might probably want to have a look at the Money gem.

The way it works, is to represent money amounts in cents and use integers. You can follow this way and store your data as integer so that you don't need to deal with Float precision.

like image 57
Simone Carletti Avatar answered Oct 18 '22 00:10

Simone Carletti


What Simone Says.

I just inserted the money gem in my project and you can store it as a Money type as well.

class Product
  include Mongoid::Document

  field :price,    type: Money
end

Money.class_eval do

  # Converts an object of this instance into a database friendly value.
  def mongoize
    [cents, currency.to_s]
  end

  class << self

    # Get the object as it was stored in the database, and instantiate
    # this custom class from it.
    def demongoize(object)
      cur = object[1] || Money.default_currency
      Money.new(object[0], cur)
    end

    # Takes any possible object and converts it to how it would be
    # stored in the database.
    def mongoize(object)
      case object
      when Money
        object.mongoize
      else object
      end
    end

    # Converts the object that was supplied to a criteria and converts it
    # into a database friendly form.
    def evolve(object)
      case object
      when Money then object.mongoize
      else object
      end
    end
  end

end   
like image 30
Michael Koper Avatar answered Oct 17 '22 22:10

Michael Koper