Ruby-on-rails has_and_belongs_to_many vs has_many 通过

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2780798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:38:21  来源:igfitidea点击:

has_and_belongs_to_many vs has_many through

ruby-on-rails

提问by Mohit Jain

Please explain the difference between has_and_belongs_to_many and has_many through relationship. When and where to use which one?

请通过关系解释has_and_belongs_to_many和has_many的区别。何时何地使用哪一种?

回答by Dan

As far as I can remember, has_and_belongs_to_manygives you a simple lookup table which references your two models.

据我所知,has_and_belongs_to_many为您提供了一个简单的查找表,其中引用了您的两个模型。

For example,

例如,

Stories can belong to many categories. Categories can have many stories.

故事可以属于许多类别。类别可以有很多故事。

Categories_Stories Table
story_id | category_id

has_many :throughgives you a third model which can be used to store various other pieces of information which don't belong to either of the original models.

has_many :through为您提供第三个模型,可用于存储不属于任何原始模型的各种其他信息。

For example

例如

Person can subscribe to many magazines. Magazines can have many subscribers.

一个人可以订阅很多杂志。杂志可以有很多订阅者。

Thus, we can have a subscription model in the middle, which gives us a similar table to the earlier example, but with additional properties.

因此,我们可以在中间有一个订阅模型,它为我们提供了与前面示例类似的表,但具有附加属性。

Subscriptions Table
person_id | magazine_id | subscription_type | subscription_length | subscription_date 

And so on.

等等。

回答by Tadas T

From http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many

来自http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and-belongs-to-many

The simplest rule of thumb is that you should set up a has_many :through relationship if you need to work with the relationship model as an independent entity. If you don't need to do anything with the relationship model, it may be simpler to set up a has_and_belongs_to_many relationship (though you'll need to remember to create the joining table in the database). You should use has_many :through if you need validations, callbacks, or extra attributes on the join model.

最简单的经验法则是,如果您需要将关系模型作为独立实体使用,您应该设置一个 has_many :through 关系。如果您不需要对关系模型做任何事情,设置 has_and_belongs_to_many 关系可能更简单(尽管您需要记住在数据库中创建连接表)。如果您需要验证、回调或连接模型上的额外属性,您应该使用 has_many :through。

回答by davidfurber

My rule of thumb is, can I get by with a list of checkboxes here? If so, then it's a habtm association. If I need the checkbox to capture more about the relationship than simply yes/no it belongs, then use has_many :through. HABTM is as simple as using the _ids method with a simple_form collection_check_boxes. Has_many :through often involves accepts_nested_attributes_for.

我的经验法则是,我可以在此处使用复选框列表吗?如果是这样,那么它是一个 habtm 协会。如果我需要复选框来捕获更多关于关系的信息,而不仅仅是它所属的是/否,那么使用 has_many :through。HABTM 就像使用带有 simple_form collection_check_boxes 的 _ids 方法一样简单。Has_many :through 经常涉及accepts_nested_attributes_for。

回答by Andre Machado

You should use has_many :through if you need validations, callbacks, or extra attributes on the join model.

如果您需要验证、回调或连接模型上的额外属性,您应该使用 has_many :through。

回答by RuboTrip

Many of the answers clarify that you should use has_and_belongs_to_manyvs. has_many through:if you will not need any extra data or validations on the join table.

许多答案阐明,如果您不需要连接表上的任何额外数据或验证,您应该使用has_and_belongs_to_manyvs。has_many through:

However, beware of taking this approach. In the early stages of application development, it is nearly impossibleto know what extra features or validations you may need in the far future of your project's lifecycle. If you decided to use has_and_belongs_to_many, and want to add one simple datapoint or validation 2 years down the road, migrating this change will be extremely difficult and bug-prone. To be safe, default to has_many :through

但是,请注意采取这种方法。在应用程序开发的早期阶段,几乎不可能知道在项目生命周期的遥远未来可能需要哪些额外的功能或验证。如果您决定使用has_and_belongs_to_many,并希望在 2 年后添加一个简单的数据点或验证,则迁移此更改将非常困难且容易出错。为安全起见,默认为has_many :through

回答by yozzz

From my experience it's always better to use has_many: throughbecause you can add timestamps to the table. Many times while debugging some ActiveRecordobjects that are connected through HABTM, I was missing created_at, updated_attimestamps to get the clue what actually happened. So keep in mind that it can help you to debug, investigate issues with data relations in context of time, because without it your are "blind" when relations were created or updated.

根据我的经验,使用总是更好,has_many: through因为您可以向表中添加时间戳。很多时候,在调试一些ActiveRecord是通过HABTM连接的对象,我失踪了created_atupdated_at时间戳得到什么实际发生的线索。因此请记住,它可以帮助您在时间上下文中调试、调查数据关系问题,因为如果没有它,您在创建或更新关系时将“视而不见”。