您在rails迁移中使用Uniq的哪一方面

问题描述 投票:0回答:1

在我的rails应用程序中,我有2个型号ProfileSkill

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
ruby-on-rails orm relationship has-and-belongs-to-many
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.