views:

1532

answers:

3

I wrongly named one column as hased_password, should have been hashed_password instead. Can i use migration to correct it?

A: 

http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

Under "Available Transformations"

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

James Manning
+7  A: 
rename_column :table, :old_column, :new_column

Try dizzy, easier for those quick references needs.

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 < ActiveRecord::Migration
  def self.up
    rename_column :table_name, :old_column, :new_column
  end

  def self.down
    # rename back if you need or do something else or do nothing
  end
end

Then just db:migrate as usual or however you go about your business.

nowk
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
I put up a quick example for you.
nowk
Many thanks for this!
AlexC
A: 

imo in this case better use rake db:rollback. Then edit your migration and again type rake db:migrate

elf.xf