Sometimes, Activerecord data types confuse me. Err, often. One of my eternal questions is, for a given case,
Should I use
:decimal
or:float
?
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:
-45.756688
, 120.5777777
, ...0.9
, 1.25
, 1.333
, 1.4143
, ...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.
This question is related to
ruby-on-rails
types
floating-point
decimal
rails-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!"
In Rails 4.1.0, I have faced problem with saving latitude and longitude to MySql database. It can't save large fraction number with float data type. And I change the data type to decimal and working for me.
def change change_column :cities, :latitude, :decimal, :precision => 15, :scale => 13 change_column :cities, :longitude, :decimal, :precision => 15, :scale => 13 end
Source: Stackoverflow.com