2013-08-06 107 views
0

是否需要在HABTM連接表上添加_index?我使用t.belongs_to(aka t.references)。這裏是代碼HABTM連接表需要索引嗎?

class CreateCohortsUsersTable < ActiveRecord::Migration 

    def change 
    create_table :cohorts_users, :id => false do |t| 
     t.belongs_to :cohort 
     t.belongs_to :user 
    end 
    add_index :cohorts_users, :cohort_id # Do I need this? 
    add_index :cohorts_users, :user_id # Do I need this? 
    end 
end 

回答

0

它通常是一個好主意,在兩列添加一個唯一索引:

add_index :cohorts_users, [:cohort_id, :user_id], unique: true 
相關問題