mysql decimal column and rails problem

Make sure that within your database migration file you specify the following for any decimal column type.

	t.decimal :total_assets, :default => 0, :precision => 8, :scale => 2

source: Decimal support in rails

Otherwise, the default schema is precision => 10, :scale => 0. If you are trying to do money, then you want your scale to be 2. That’s the way money works. Two decimal places. Your precision could be anything, but I followed prag dave and went with 8.

ALSO

Make sure your schema file matches this db migration file. Otherwise, you still might have problems. mysql decimal production problems

Thought: It seems a little ridiculous that the decimal’s scale for rails in mysql defaults to 0. If you are using a decimal (instead of an integer type) then it is almost certain you want at least one decimal, and it is very likely you want two decimals to deal with money. It would be nice if rails would default to a scale of 2 on a decimal column type.

Comments