0

i have a migration file

class CreateProductDetails < ActiveRecord::Migration

    def change
        create_table :product_details do |t|
        t.integer :quantity , :default => 1
        t.float :price , :default => 0.0
        t.float :fee , :default => 0.0    
        t.timestamps            
      end          
    end    
end

this table has been used for a long time and holds important data that i dnt want to loose.Now i want to scale and add default value(0.00) to the colm price/fee.i generated a new migration file such as:-

class AddScaleToProductDetail < ActiveRecord::Migration
   def self.up
   change_column :product_details, :price, :float, :default => 0.00, :scale =>2
   change_column :product_details, :fee, :float, :default => 0.00, :scale =>2
  end
end

now i ran the migration :-

[root@www project_trunk]# rake db:migrate
==  AddScaleToProductDetail: migrating ====================================
-- change_column(:product_details, :price, :float, {:default=>0.0, :scale=>2})
   -> 0.6311s
-- change_column(:product_details, :fee, :float, {:default=>0.0, :scale=>2})
   -> 0.0480s
==  AddScaleToProductDetail: migrated (1.3898s) ===========================

...now i have few questions...

  • why i am still getting to see default-0.0 when migration ran,

  • moreover any newly inserted values are still getting saved as 22 instead of 22.00(expected).

  • i want to update the existing values to a decimal place of 2.

1 Answers1

0

Try specifying precision:

change_column :product_details, :price, :float, :default => 0.00, precision: 6, :scale =>2

Or more precision if you want, remember:

1234.12
precision = 6
scale = 2

To check you are getting the correct results, in SQL server console:

use your_database;
explain your_table;

You should se something like this

product_details       | float(6,2)
sites
  • 21,417
  • 17
  • 87
  • 146