Rails 中的 has_and_belongs_to_many
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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:
therefore:
如果您不需要连接模型,那么使用
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.我永远不会使用 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.
我是这样想的。 假设您已经发现需要多对多模型:
(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->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.