Rails: How to add add_index to existing table

yoshyosh picture yoshyosh · May 24, 2011 · Viewed 13.2k times · Source

I already migrated a table called units with several columns. I was wondering how to migrate in a stand alone 'add_index' to this table using the cmd. Is this code correct:

class AddIndexToUnits < ActiveRecord::Migration
  def self.up
    add_index :units, :lesson_id
  end

  def self.down
    remove :units
  end
end

I have a feeling the self.down could be wrong, I am unsure.

Answer

Kris picture Kris · May 24, 2011

The self.up method is correct. Use this for your self.down:

remove_index :units, :column => :lesson_id