How to rename a column in rails?

less than 1 minute read

Syntax

rename_column(table_name, column_name, new_column_name)

Example

rename_column(:users, :email_id, :email)

Rails Command:

$ rails generate migration rename_email_id_to_email
      invoke  active_record
      create    db/migrate/20230906131708_rename_email_id_to_email.rb

The above command will generate a migration

class RenameEmailIdToEmail < ActiveRecord::Migration[7.1]
  def change
  end
end

We need to modify it to:

class RenameEmailIdToEmail < ActiveRecord::Migration[7.1]
  def change
    rename_column(:users, :email_id, :email)
  end
end