[ruby-on-rails] Float vs Decimal in ActiveRecord
In Rails 3.2.18, :decimal turns into :integer when using SQLServer, but it works fine in SQLite. Switching to :float solved this issue for us.
The lesson learned is "always use homogeneous development and deployment databases!"
Sometimes, Activerecord data types confuse me. Err, often. One of my eternal questions is, for a given case,
Should I use
I've often come across this link, ActiveRecord: :decimal vs :float?, but the answers aren't quite clear enough for me to be certain:
I've seen many threads where people recommend flat out to never use float and always use decimal. I've also seen suggestions by some people to use float for scientific applications only.
Here are some example cases:
I have used
:decimal in the past, but I found dealing with
BigDecimal objects in Ruby was unnecessarily awkward as compared to a float. I also know I can use
:integer to represent money/cents, for example, but it doesn't quite fit for other cases, for example when quantities in which precision could change over time.
- What are the advantages/disadvantages of using each?
- What would be some good rules of thumb to know which type to use?