Rails:如何将add_index添加到现有表

问题描述:

我已经迁移了一个名为units的表,其中有几列。我想知道如何使用cmd将独立的add_index迁移到此表中。这段代码是否正确:

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

我感觉self.down可能是错的,我不确定。

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

self.up方法是正确的。用于你的self.down:

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

remove_index :units, :column => :lesson_id