3
 rails g migration CreateJoinTable zombie:index role:index

This creates this migration:

class CreateJoinTable < ActiveRecord::Migration
  def change
    create_join_table :zombies, :roles do |t|
      t.index [:zombie_id, :role_id]
      t.index [:role_id, :zombie_id] # I'd be happy if it didn't have this!
    end
  end
end

That migration is nearly there, but why do I have four indexes rather than two? Where in my generate command does it specify to create an extra two sets of indexes for indexes that already exist?

Starkers
  • 10,273
  • 21
  • 95
  • 158

2 Answers2

5

Try this instead:

rails g migration CreateJoinTableRolesZombies roles zombies

The migration comments out the indexes, presumably to show that the create_join_table handles this for you.

Note that in rails 4 the table names must be in sort order. Also, the migration name has been expanded in this example just to make it clear. CreateJoinTable appears in it, which is sufficient.

Phil
  • 2,797
  • 1
  • 24
  • 30
  • for reference: http://edgeguides.rubyonrails.org/active_record_migrations.html#creating-a-join-table and also this: http://edgeguides.rubyonrails.org/active_record_migrations.html#creating-a-standalone-migration (last part of this section) – sixty4bit Aug 21 '15 at 14:18
1

You've only two indexes, though it might index more than it should. See Index on multiple columns in RoR to explain the array syntax and how that changes t.index.

Community
  • 1
  • 1
Louis St-Amour
  • 4,065
  • 1
  • 30
  • 28