在我的rails应用程序中,我有2个型号Profile
和Skill
。
Profile
has_and_belongs_to_many Skill
,只能有一次相同的Skill
。
Skill
has_and_belongs_to_many Profile
。如果我们尊重第一个关系,那么它应该不会超过一次相同的Profile
。
当我创建连接表时,我有两种可能性:
rails g migration CreateProfilesSkillsJoinTable profiles:uniq skills
要么
rails g migration CreateProfilesSkillsJoinTable profiles skills:uniq
第一个选项将生成
class CreateProfilesSkillsJoinTable < ActiveRecord::Migration[5.1]
def change
create_join_table :profiles, :skills do |t|
t.index [:profile_id, :skill_id], unique: true
# t.index [:skill_id, :profile_id]
end
end
end
第二个会生成
class CreateProfilesSkillsJoinTable < ActiveRecord::Migration[5.1]
def change
create_join_table :profiles, :skills do |t|
# t.index [:profile_id, :skill_id]
t.index [:skill_id, :profile_id], unique: true
end
end
end