Renaming Columns in Rails

The proper way to rename a column in Rails is to generate a migration. This migration should be named ChangeOldColumnNameToNewColumnName.

As an example, say that you want to generate a migration that renames the column checking_acct_balance to acct_balance in the balances table. To do this, first generate a migration named using the following pattern:

rails generate migration ChangeCheckingAcctBalanceToAcctBalance
# creates   db/migrate/xxxxxxxxxx_change_checking_acct_balance_to_acct_balance.rb

Then, open the migration that was just created at db/migrate/xxxxxxxxxx_change_checking_acct_balance_to_acct_balance.rb and edit it to rename the columns.

class ChangeCheckingAcctBalanceToAcctBalance < ActiveRecord::Migration
  def change
    rename_column :balances, :checking_acct_balance, :acct_balance

The patten for the rename_column portion is

rename_column :table_name, :old_column_name, :new_column_name

Note: The def change method is only available in Rails 3.1 and above. For versions of Rails prior to Rails 3.1, the def up and def down methods must be used to explicitly describe the up and down migrations.