Rails 3.2
MySQL gem
I have the following in my migration:
t.decimal :pre_tax_total, default: nil, scale: 2
t.decimal :post_tax_total, default: nil, scale: 2
Based on what I read, scale:2 will produce a decimal with 2 trailing digits.
When I run the migration, and look at the table structure, I see the following:
pre_tax_total decimal(10,0)
post_tax_total decimal(10,0)
Which means the values are getting truncated by the MySQL server. What is the ActiveRecord syntax to create these columns as decimal(10,2)?
That would be:
t.decimal :pre_tax_total, precision: 10, scale: 2
Although Rails 3 Migrations Guide skips it, the description is available in the source code:
# Note: The precision is the total number of significant digits, # and the scale is the number of digits that can be stored following # the decimal point. For example, the number 123.45 has a precision of 5 # and a scale of 2. A decimal with a precision of 5 and a scale of 2 can # range from -999.99 to 999.99. # # Please be aware of different RDBMS implementations behavior with # <tt>:decimal</tt> columns: # * The SQL standard says the default scale should be 0, <tt>:scale</tt> <= # <tt>:precision</tt>, and makes no comments about the requirements of # <tt>:precision</tt>. # * MySQL: <tt>:precision</tt> [1..63], <tt>:scale</tt> [0..30]. # Default is (10,0).
The last line partly explains why you got decimal(10,0)
.