What is the behaviour of create_join_table of ActiveRecord::Migration?

Called without any block, create_join_table just creates a table with two foreign keys referring to the two joined tables.

However, you can actually pass a block when you call the method to do any additional operations (say, adding indexes for example). From the Rails doc:

create_join_table :products, :categories do |t|
  t.index :product_id
  t.index :category_id
end

Have a look at create_join_table documentation.

You can check the create_join_table code at the bottom (click on Source: show).


SchemaStatements#create_join_table() only creates join table without any fancy indexes etc,... So if you wish to use uniqueness constraint on two fields you have to do something like this:

class CreateJoinTable < ActiveRecord::Migration
  def change
    create_join_table :posts, :users do |t|
      t.integer :post_id, index: true
      t.integer :user_id, index: true
      t.index [:post_id, :user_id], name: 'post_user_un', unique: true
    end
  end
end

Please also note that create_join_table by default does NOT create id field.