I have a Rails application that defines a migration containing a decimal value with an accuracy of 8 and a scale of 2. The database I installed is PostgreSQL 9.1 database.
class CreateMyModels < ActiveRecord::Migration def change create_table :my_models do |t| t.decimal :multiplier, precison: 8, scale: 2 t.timestamps end end end
When I run rake db:migrate , the migration is successful, but I noticed an error when I tried to start MyModel.find_or_create_by_multiplier . If I execute the following command twice, the object will be created twice:
MyModel.find_or_create_by_multiplier(multiplier: 0.07)
I assume that this should create an object during the first call, and then find the object during the second call. Unfortunately, this does not look like a multiplier set to 0.07.
This DOES works as expected for every other number that I selected with the specified command. The following commands work as expected (creating an object during the first call, and then searching for the object during the second call).
MyModel.find_or_create_by_multiplier(multiplier: 1.0) MyModel.find_or_create_by_multiplier(multiplier: 0.05) MyModel.find_or_create_by_multiplier(multiplier: 0.071)
When I look at the description of the PostgreSQL database of the MyModel table, I notice that the table does not have a limit on the number column.
Column | Type | Modifiers -------------+-----------------------------+------------------------------------------------------- id | integer | not null default nextval('my_models_id_seq'::regclass) multiplier | numeric | created_at | timestamp without time zone | not null updated_at | timestamp without time zone | not null
My db / schema.rb also does not specify accuracy and scale:
ActiveRecord::Schema.define(:version => 20121206202800) do ... create_table "my_models", :force => true do |t| t.decimal "multiplier" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end ...
So, my first question is: why do not I see the accuracy and scaling applied to PostgreSQL during migration? The documentation states that it should be supported .
My second question is: why is 0.07 being incorrectly compared using the command MyModel.find_or_create_by_multiplier(multiplier: 0.07) ? (If I need to open another question for this, I will).