Flowdock
method

index

Importance_5
index(column_name, options = {}) public

Adds a new index to the table. column_name can be a single Symbol, or an Array of Symbols.

t.index(:name)
t.index([:branch_id, :party_id], unique: true)
t.index([:branch_id, :party_id], unique: true, name: 'by_branch_party')

See {connection.add_index}[rdoc-ref:SchemaStatements#add_index] for details of the options you can use.

Show source
Register or log in to add new notes.
September 16, 2010
1 thank

Re: Doesn't work? Don't think it ever has.

Instead of

create_table :user_follows, :force => true do |t|
  t.references :user
  t.references :followed_user
  t.timestamps
  t.index :user
  t.index :followed_user
end

Try

create_table :user_follows, :force => true do |t|
  t.references :user
  t.references :followed_user
  t.timestamps
end

index :user_follows , :user
index :user_follows , :followed_user

It looks like the provided examples are incorrect…

October 22, 2010
1 thank

still broken

add_index is a different method. I think this is just a bug and it’s broken.

November 29, 2011
0 thanks

index doesn't work

@ssoroka even I’m having same issue. how did you resolve it?

February 15, 2012 - (<= v2.3.8)
0 thanks

@ssoroka and @drova and future readers

I guess these two have already found a solution, but future readers might have not. index and references do not map perfectly

change_table :foo do |t|
  t.references :bar
  t.index      :bar_id
end

references gets the model name while index gets the column name.

February 15, 2012 - (<= v2.3.8)
0 thanks

Not working in create_table

When using the index method within a create_table statement, it does not have any side effect - at least not in MySQL.

create_table :comment do |t|
  t.belongs_to :post

  t.timestamps

  # not working inside create_table !
  t.index :post_id
end

It is working properly in change_table though

change_table :comment do |t|
  t.belongs_to :user

  # this works inside change_table
  t.index :user_id
end

Unfortunately this flaw is not reported in any way. The index is just not created.

I have only tested this with the mysql2 driver in Rails 2.3.x. I’m not sure, if this happens in other versions/adapters as well.

September 23, 2010
0 thanks

Or maybe...

or maybe

add_index :user_follows , :user
add_index :user_follows , :followed_user
June 4, 2012
0 thanks

This method does not work.

It’s an old problem, reported back in 2010, just reopened issue:

http://github.com/rails/rails/issues/6620

September 9, 2010
0 thanks

Doesn't work? Don't think it ever has.

This doesn’t work for me. I do something like:

create_table :user_follows, :force => true do |t|
  t.references :user
  t.references :followed_user
  t.timestamps
  t.index :user
  t.index :followed_user
end

and I get:

rake aborted!
An error has occurred, all later migrations canceled:

undefined method `index' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x106c02220>

add_index has the same effect.