How to rename a database column in rails using migration?

Rename_column :table, :old_column, :new_column Update: You'll probably want to create a separate migration to do this. (Rename FixColumnName as you will) script/generate migration FixColumnName # creates db/migrate/xxxxxxxxxx_fix_column_name. Rb Then edit the migration to do your will.

# db/migrate/xxxxxxxxxx_fix_column_name. Rb class FixColumnName Down # rename back if you need or do something else or do nothing end end An update for Rails 3.1 While, the up and down methods still apply. Rails 3.1 receives a change method that "knows how to migrate your database and reverse it when the migration is rolled back without the need to write a separate down method" rails g migration FixColumnName class FixColumnName Class FixColumnNames Then just db:migrate as usual or however you go about your business.

1 Thanks! Can I know how do I make use of this command? Do I need to add any code to the migration file or there is a command that I need to execute from terminal?

Thanks – user228002 Jan 2 '10 at 16:36 I put up a quick example for you. – kwon Jan 2 '10 at 17:13 Many thanks for this! – AlexC Sep 25 '10 at 1:45 self.

Down should always be the opposite of self. Up, so "if you need or do something else or do nothing" isn't really recommended. Just do: rename_column :table_name, :new_column, :old_column – Luke Griffiths Aug 29 at 15:21 While it's normal practice to revert what you did in self.

Up I would not say self. Down "should always be opposite". In depends on the context of your migration.

Just putting the "opposite" might not be the "right" down migration. – kwon Aug 29 at 16:36.

api.rubyonrails.org/classes/ActiveRecord... Under "Available Transformations" rename_column(table_name, column_name, new_column_name): Renames a column but keeps the type and content.

From API: rename_column(table_name, column_name, new_column_name): Renames a column but keeps the type and content.

Imo in this case better use rake db:rollback. Then edit your migration and again type rake db:migrate. However, if you have data in the column you don't want to lose, then use rename_column.

2 This idea doesn't work if the migration has already been run by other people. – Ash Berlin May 9 at 16:04.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions