ruby-on-railsrubycurrency

Ruby and money, in a rails app, how to store money values in the db?


I want to make sure I don't have rounding issues when it comes to storing prices for products in a rails app.

What mysql datatype should I use, and what does it map to in rails?

I want decimal with 10 places for precision.


Solution

  • Sounds you want the :decimal column type. You can control the total number of digits and the number of decimal places with the :precision and :scale options:

    add_column :mytable, :mycolumn, :decimal, :precision => 30, :scale => 10
    

    A little more on data types in this documentation.