Rails: How to add add_index to existing table Rails: How to add add_index to existing table ruby-on-rails ruby-on-rails

Rails: How to add add_index to existing table


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

remove_index :units, :column => :lesson_id


Almost

class AddIndexToUnits < ActiveRecord::Migration  def self.up    add_index :units, :lesson_id, :name=>'lesson_index'  end  def self.down    remove_index :units, 'lesson_index'  endend


To remove an index, you must use remove_index with the same table and column specification as the self.up's add_index has. So:

def self.down  remove_index :units, :lesson_idend

A multi-column index example would be:

def self.down  remove_index :units, [:lesson_id, :user_id]end