Wednesday, April 25, 2012

Change Column or column type in Rails

Hi, 

If you have to change the column_type of an existing column in a table, you have to create a new migration and do the following changes:

saurav@saurav-Vostro-1540:~/workspace/project$./script/generate migration ChangeNotes

The above command will generate a new migration file and the migration should be like this after you have made the change:

class ChangeNotes < ActiveRecord::Migration
  def self.up
      change_table :notes  do |t|
        t.change :note, :text
       end
    end

    def self.down
      change_table :notes do |t|
        t.change :note, :string
      end
    end
end

where "note" is the column for which I have changed the data-type. Earlier, the data type of "note" column was set to "String" and now it has been set to "text"..