Rails 中的 has_and_belongs_to_many

发布于 2024-07-15 04:43:47 字数 487 浏览 5 评论 0原文

在 Rails 中使用 has_and_belongs_to_many 关联而不是 has_many :through 有什么明显的错误吗? 我知道这些 文章 描述差异和解决方法,但它们来自 2006 年。从我读过的内容来看,它似乎人们认为 habtm 又旧又笨重,但是如果您正在寻找一个不需要模型的简单多对多连接,该怎么办?

想法?

Is there anything explicitly wrong with using has_and_belongs_to_many associations in rails instead of has_many :through? I'm aware of these articles 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?

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魄砕の薆 2024-07-22 04:43:47

has_and_belongs_to_many 适用于简单的多对多关系。

另一方面, has_many :through 表示间接的一对多关系或多对多关系与属性。

如果您只是在寻找简单的多对多关系,我看不出有任何理由不使用 has_and_belongs_to_many 。

多对多关系示例:

用户属于零个或多个组,并且组有零个或多个成员(用户)。

与属性的多对多关系示例:

用户属于零个或多个组,并且组有零个或多个具有等级的成员。

例如,Alice 可能是 A 组中的管理员,B 组中的主持人。您可以在联接表中保存此属性。

间接一对多关系示例:

一个类别有零个或多个子类别,每个子类别有零个或多个项目。

因此,一个类别通过其子类别具有零个或多个项目。

考虑以下类别:

食物 → 水果、蔬菜
水果→苹果、橙子等
蔬菜→胡萝卜、芹菜等

因此:

食物→苹果、橙子、胡萝卜、芹菜等

has_and_belongs_to_many is meant for simple many-to-many relationships.

has_many :through, on the other hand, is meant for indirect one-to-many relationships, or many-to-many relationships with properties.

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.

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.

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.

therefore:

Food → Apple, Orange, Carrot, Celery, etc.

禾厶谷欠 2024-07-22 04:43:47

如果您不需要连接模型,那么使用 has_and_belongs_to_many 没有任何问题。 我自己刚刚在最近的一个项目中使用过它。

There's nothing wrong with using has_and_belongs_to_many if you don't require a join model. I've just used it myself on a recent project.

你是我的挚爱i 2024-07-22 04:43:47

我永远不会使用 HABTM,不是因为担心优雅,而是因为我总能想象到将来想要向关系中添加数据,即使我现在看不到这一点。 由于懒惰,我希望能够仅将列添加到联接中,而不必重新处理关系,然后添加列。

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. 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.

唠甜嗑 2024-07-22 04:43:47

我是这样想的。 假设您已经发现需要多对多模型:

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

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

如果不需要存储信息,请使用 HABTM 关系关于我上面的(希望是可识别的)图片中的每一行。

如果您需要存储有关这些线路(关系)的信息,请使用“通过”。

因此,如果您只是说人员 [XYZ] 拥有并属于项目 [123],但不需要说明项目 1 上人员 X 的任何信息,请使用 HABTM。

如果您想说某人 X 拥有项目 1,并且在给定日期被分配该项目,那么您突然就拥有了适合该特定关系的正确关系,并且可以更好地使用 HMT。

I think about it this way. Assuming you've already found that you need a many-to-many model:

X----1
  __/
 /
Y----2
  __/
 /  
Z----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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文