change column type in rails with example

 class ChangePhoneToBeStringInCustomers < ActiveRecord::Migration[5.0]

  def up
    change_column :customers, :phone, :string
  end

  def down
    change_column :customers, :phone, :integer
  end
end


The code we need to add takes the form of

  change_column :table_name, :column_name, :new_type

In our case it would be something like

  change_column :customers, :phone, :string

The completed file will look like this

class ChangePhoneToBeStringInCustomers < ActiveRecord::Migration[5.0]
  def change
    change_column :customers, :phone, :string
  end
end

Comments

Popular posts from this blog

Rails 7 Features :: Comparison with Rails 6 and Rails 5