Wednesday, July 27, 2011

renaming a database column using rails generate migration

To rename a database column using rails generate migration command:
1. run "rails generate migration 'RenameDatabaseColumn'"
2. This will create a database migration file in db/migrate/20110727115656_rename_database_column

class RenameDatabaseColumn < ActiveRecord::Migration
def self.up
rename_column :table, :old_column, :new_column
end

def self.down
rename_column :table, :new_column, :old_column
end
end

3. Then just run "rake db:migrate" to rename the column
4. To roll back just run "rake db:rollback"