[ruby-on-rails] Rails: Adding an index after adding column

Suppose I created a table table in a Rails app. Some time later, I add a column running:

rails generate migration AddUser_idColumnToTable user_id:string. 

Then I realize I need to add user_id as an index. I know about the add_index method, but where should this method be called? Am I supposed to run a migration (if yes, which one ?), then adding by hand this method?

This question is related to ruby-on-rails indexing migration

The answer is


You can run another migration, just for the index:

class AddIndexToTable < ActiveRecord::Migration
  def change
    add_index :table, :user_id
  end
end

Similar questions with ruby-on-rails tag:

Similar questions with indexing tag:

Similar questions with migration tag: