简体   繁体   English

Ruby on Rails:在has_many:through和has_many之间进行选择

[英]Ruby on Rails: choosing between has_many :through and simply has_many

I'm trying to create a linkedin clone, in which users can have several skills. 我正在尝试创建一个Linkedin克隆,用户可以在其中具有多种技能。 They can also have descriptions for each skill and choose whether the skill is their primary skill or secondary skill (they can have only one of each). 他们还可以对每种技能进行描述,并选择该技能是他们的主要技能还是次要技能(他们只能具有一项)。

I can't decide whether to just use a has_many or has_many :through. 我不能决定是否只使用has_many或has_many:through。

If I use has_many 如果我使用has_many

class Skill < ActiveRecord::Base
    belongs_to :user

    validates :description, presence: true
    validates :name, presence: true
end

class User < ActiveRecord::Base
    has_many :skills, dependent: :destroy
end

Skills table will also have columns primary and secondary, which are booleans. Skills表还将具有primary和secondary列,它们是布尔值。


If I use has_many :through 如果我使用has_many:through

class Skill < ActiveRecord::Base
    has_many :users, through: :users_skills
    has_many :users_skills
end

class UsersSkill < ActiveRecord::Base
    belongs_to :user
    belongs_to :skill
end

class User < ActiveRecord:Base
    has_many :skills, through: :users_skills
    has_many :users_skills
end

Here, I will have the primary and secondary boolean columns in the UsersSkill model. 在这里,我将在UsersSkill模型中拥有主要和次要的布尔列。

Which do you think would be a better choice? 您认为哪一个是更好的选择?

Based on both your stated requirements: 根据您的既定要求:

I mainly want people to be able to type in a skill to search for people with that skill 我主要希望人们能够输入一项技能来搜索具有该技能的人

They can also have descriptions for each skill and choose whether the skill is their primary skill or secondary skill (they can have only one of each). 他们还可以对每种技能进行描述,并选择该技能是他们的主要技能还是次要技能(他们只能具有一项)。

And on just my understanding of the meaning of the models, ie that a "Skill" is something which doesn't necessarily belong to a single user -- I'd go with has_many :through. 仅凭我对模型含义的理解,即“技能”并不一定属于单个用户-我会使用has_many:through。

Reason for this is that the skills are things that stand on their own (and as you said, you want to be able to search for them). 原因是这些技能是独立存在的(就像您说的那样,您希望能够搜索它们)。 Just because one person lists, say, "Scrubbing Floors" as their primary skill, and another as their secondary skill, doesn't mean you should have two entries with "Scrubbing Floors". 仅仅因为一个人将“ Scrubbing Floors”列为主要技能,而将另一个列为其次要技能,并不意味着您应该在“ Scrubbing Floors”中有两个条目。 Have a single Skill for it, and use UsersSkills to assign it to multiple users, along with any information specific to that user's instance of the skill. 拥有一项技能,然后使用UsersSkills将其分配给多个用户,以及该用户实例所特有的任何信息。

You can put the primary/secondary ranking on the users_skills table. 您可以将主要/次要排名放在users_skills表上。 The existence of this model will also save you a lot of effort in the future should you ever need to expand the information stored in these mappings. 如果您需要扩展存储在这些映射中的信息,此模型的存在还将在将来为您节省很多精力。

The bottom line is that the choice depends on one piece of functionality of your system - whether a users' skills are specific to them, or part of a wider group 最重要的是,选择取决于系统的一项功能- 用户的技能是特定于他们的,还是属于更广泛的群体的一部分

  • The has_many relationship will be good if your user will add their skills themselves 如果您的用户自己添加技能,则has_many关系会很好
  • The has_many :through relationship will be good if your user's skills can be selected from a wider pool 如果可以从更广泛的范围中选择用户的技能,则has_many :through关系会很好

-- -

System 系统

You have to remember that since Rails is object-orientated , the setup of your ActiveRecord associations is primarily based on the association of the objects in your system. 您必须记住,由于Rails是面向对象的 ,因此ActiveRecord关联的设置主要基于系统中对象的关联。

many-to-many associations are primarily based on the sharing of objects between two models; many-to-many关联主要基于两个模型之间的对象共享 whilst one-to-many associations are the result of a model having lots of dependent data one-to-many关联是具有大量相关数据的模型的结果

To make the decision, I would simply look at what you've got. 为了做出决定,我只想看看您有什么。 If you want to pre-populate the skills, as per the likes of LinkedIn, go for the has_many :through 如果您想像LinkedIn一样预先填充技能,请使用has_many :through

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM