You can easily use migration to do this, and Rails will generate some code for you.
At the command line, create a new migration:
rails generate migration change_price_column_to_decimal
Rails will create a migration in the db/migrate directory. The file name will be the timestamp followed by _change_price_column_to_decimal.rb .
In the generated migration, you add the up and down methods to convert the field:
class ChangePriceColumnToDecimal < ActiveRecord::Migration def up change_column :products, :price, :decimal, :precision => 15, :scale => 2, null: false end def down # Either change the column back, or mark it as irreversible with: raise ActiveRecord::IrreversibleMigration end end
To migrate, run the appropriate rake command from the command line:
rake db:migrate
This will transform the database for you. Keep in mind that when converting from a floating point to a decimal string, you will lose a few significant digits, depending on what you set to scale , although if you are dealing with product prices, this will probably not be a big problem.
source share