Does PostgreSQL support precision and scaling for Rails?

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).

+4
source share
2 answers

This confuses...

I have accuracy with an error.

Change the transition to:

 t.decimal :multiplier, precision: 8, scale: 2 

everything is fixed.

+7
source

PostgreSQL 9.1 allows you to declare a column in any of these ways.

 column_name decimal column_name numeric column_name decimal(8, 2) column_name numeric(8, 2) 

If you look at this column using, say, pgAdminIII, it will show you exactly how it was created. If you (or Rails) created the column as numeric , it will indicate "numeric". If you (or Rails) created the column as decimal(8, 2) , it will say "decimal (8, 2)".

Therefore, it seems to me that Rails does not convey the accuracy and scaling of PostgreSQL. Instead, it simply tells PostgreSQL to create this numerical column. Rails docs suggest not doing this.

The syntax example in this link is different from yours.

 td.column(:bill_gates_money, :decimal, :precision => 15, :scale => 2) 
+2
source

All Articles