简体   繁体   English

在 Rails 中 has_and_belongs_to_many

[英]has_and_belongs_to_many in Rails

Is there anything explicitly wrong with using has_and_belongs_to_many associations in rails instead of has_many:through?在 rails 中使用 has_and_belongs_to_many 关联而不是 has_many:through 有什么明显错误吗? I'm aware of thesearticles describing differences and work arounds, but they are from 2006. From things I've read on SO, it seems like people think that habtm is old and clunky, but what if a simple many to many join with no model necessary is what you're looking for?我知道这些描述差异和解决方法的文章,但它们是从 2006 年开始的。从我在 SO 上阅读的内容来看,似乎人们认为 habtm 既旧又笨重,但如果一个简单的多对多加入没有 model 必要的是你在找什么?

Thoughts?想法?

has_and_belongs_to_many is meant for simple many-to-many relationships. has_and_belongs_to_many 用于简单的多对多关系。

has_many:through, on the other hand, is meant for indirect one-to-many relationships, or many-to-many relationships with properties.另一方面,has_many:through 用于间接的一对多关系,或与属性的多对多关系。

If you're only looking for a simple many-to-many relationship, I can't see any reason not to use has_and_belongs_to_many.如果您只是在寻找简单的多对多关系,我看不出有任何理由不使用 has_and_belongs_to_many。

Example many-to-many relationship:多对多关系示例:

User belongs to zero or more groups, and group has zero or more members (users).用户属于零个或多个组,组有零个或多个成员(用户)。

Example many-to-many relationship with properties:与属性的多对多关系示例:

User belongs to zero or more groups, and group has zero or more members with ranks.用户属于零个或多个组,组有零个或多个具有等级的成员。

For example, Alice might be an Administrator in Group A, and a Moderator in Group B. You can hold this property in the join table.例如,Alice 可能是组 A 中的管理员和组 B 中的主持人。您可以在连接表中保留此属性。

Example indirect one-to-many relationship:间接一对多关系示例:

A category has zero or more sub-categories, and each sub-category has zero or more items.一个类别有零个或多个子类别,每个子类别有零个或多个项目。

A category therefore has zero or more items through its sub-categories.因此,一个类别通过其子类别具有零个或多个项目。

Consider these categories:考虑以下类别:

Food → Fruits, Vegetables食物 → 水果、蔬菜
Fruits → Apple, Orange, etc.水果→苹果、橙子等
Vegetables → Carrot, Celery, etc.蔬菜→胡萝卜、Celery等

therefore:所以:

Food → Apple, Orange, Carrot, Celery, etc.食品→苹果、橙子、胡萝卜、Celery等

I think about it this way.我是这样想的。 Assuming you've already found that you need a many-to-many model:假设您已经发现需要多对多 model:

X----1
  __/
 /
Y----2
  __/
 /  
Z----3

(x->1 y->1,2 z->2,3) (x->1 y->1,2 z->2,3)

Use a HABTM relationship if you do NOT need to store information about each of the lines in my (hopefully recognizable) picture above.如果您不需要存储关于我上面(希望可识别)图片中每一行的信息,请使用 HABTM 关系。

If you need to store information about those lines (relationships), then use a "through".如果您需要存储有关这些行(关系)的信息,请使用“通过”。

So if you are just saying that people [XYZ] have and belong to projects [123] but do not need to say anything about person X on project 1, use a HABTM.因此,如果您只是说人 [XYZ] 拥有并属于项目 [123],但不需要在项目 1 中提及人 X,请使用 HABTM。

If you want to say that person X has project 1 and was assigned that project on a given date , you suddenly have a properly for that particular relationship and better use HMT.如果你想说那个人 X 有项目 1 并且在给定日期被分配了那个项目,那么你突然之间就有了一个适合这种特定关系并更好地使用 HMT。

There's nothing wrong with using has_and_belongs_to_many if you don't require a join model.如果您不需要加入 model,则使用has_and_belongs_to_many没有任何问题。 I've just used it myself on a recent project.我刚刚在最近的一个项目中使用了它。

I would never use HABTM not because of any concern about elegance but because I can always imagine wanting to add data to a relationship in the future even if I can't see the point now.我永远不会使用HABTM,不是因为担心优雅,而是因为我总是可以想象将来想在关系中添加数据,即使我现在看不到重点。 Being lazy I would like to be able to just add the columns to the join rather than having to rework the relationships and then add the columns.懒惰的我希望能够将列添加到连接中,而不必重新处理关系然后添加列。

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

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