的has_many:通过与has_and_belongs_to_many Rails中 [英] has_many :through with has_and_belongs_to_many in Rails

查看:419
本文介绍了的has_many:通过与has_and_belongs_to_many Rails中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails的 - 什么是使用的has_many的效果:通过与has_and_belongs_to_many? 考虑有两种模式 - 具有邮电标签一多对一对多的关系如下图所示:

In Rails - what is the effect of using has_many :through with has_and_belongs_to_many? Consider having two models - Posts and Tags which have a many-to-many relationship as indicated below:

class Tag < ActiveRecord::Base
  has_many :posts_tag
  has_and_belongs_to_many :posts
end

class Post < ActiveRecord::Base
  has_many :posts_tag
  has_many :tags,  :through => posts_tag
end

class PostsTag < ActiveRecord::Base
  belongs_to :tag
  belongs_to :post
end

我用 has_​​and_belongs_to_many 的原因是因为标记属于多个职位。

The reason I use has_and_belongs_to_many is because a tag belongs to many posts.

我也考虑了的Rails协会指南,看到他们不提这种情况下,对于很多一对多的关系。但是,我没有尝试这一点,其运行在Rails中没有取得任何行为,并从我建立的小型测试数据库,似乎也返回正确的结果 post.tags tag.posts - 其中标记请参阅邮政标签型号分别为实例。

I did look into the Rails Association guide and see that they don't mention this case for a many-to-many relationship. I, however, did try this and running it in Rails didn't yield any behavior and from the small test database that I built, also seemed to return the correct results for post.tags and tag.posts - where post and tag refer to an instance of the Post and Tag models respectively.

这是正确的用法还是有任何副作用的影响,我不知道呢?此外,如果它是正确的,是实现这一目标的这Rails的方法是什么?

Is this correct usage or does it have any side affects that I am not aware of? Also, if it is correct, is this the Rails way of achieving this?

谢谢!

推荐答案

您使用 has_​​and_belongs_to_many 只有当你设置一个多一对多的关联(换句话说,当对方也有 has_​​and_belongs_to_many )。这是该协会的意义。

You use has_and_belongs_to_many only when you're setting a many-to-many association (in other words, when the other side also has has_and_belongs_to_many). That is the meaning of this association.

您应该有

class Tag < ActiveRecord::Base
  has_many :posts_tags
  has_many :posts, :through => :post_tags
end

class PostsTag < ActiveRecord::Base
  belongs_to :tag
  belongs_to :post
end

class Post < ActiveRecord::Base
  has_many :posts_tags
  has_many :tags, :through => :posts_tags
end

请注意,我用的是复数, post_tags (因为这是正确的方式)。

Notice that I used the plural, post_tags (because this is the correct way).

如果您有类似的情况在您的评论,你应该有一个

If you have the situation like in your comment, you should have a

belongs_to :post_tag

发表模式,

has_many :posts

PostTag 模式。

您现在可以问:我为什么要使用 belongs_to的:post_tag 它没有属于标签,它的标记所以,我不应该用 HAS_ONE:post_tag ?这也是我的第一个问题,但后来我想通它的轨道可能并不总是完美体现了英语语言。你需要在你的后的 post_tag_id belongs_to的预计这一点。在另一方面, HAS_ONE 希望有一列名为的post_id 是在其他present 的一面,那就是在你的 post_tag 。但是,这是不可能的,因为 post_tag 有许多职位(而不是只有一个),所以的ID不能在 post_tags 。

You may ask now: "Why should I use belongs_to :post_tag? It doesn't belong to a tag, it has a tag. So, shouldn't I use has_one :post_tag?". This was also my question at first, but then I figured that it Rails cannot always perfectly suit the english language. You need the post_tag_id column on your post, and belongs_to expects exactly that. On the other hand, has_one would expect that a column named post_id is present on the other side, that is in your post_tag. But this would be impossible, because post_tag has many posts (not only one), so the post IDs cannot be held in post_tags.

更新
仅在方法会为您提供协会之间的区别和选项,你可以通过在(在Rails的一个解释指导有关协会)。例如, HAS_ONE belongs_to的有相同的方法:

Update:
The difference between associations are only in the methods you are provided and options you can pass in (the one explained in the Rails guide on associations). For example, has_one and belongs_to have the same methods:

association(force_reload = false)
association=(associate)
build_association(attributes = {})
create_association(attributes = {})

但是,例如,方法协会= create_association 意味着关于那里的外键应该是不同的东西(就像我上面的解释)。

But, for example, methods association= and create_association imply different things concerning where the foreign key should be (like I explained above).

has_​​and_belongs_to_many 的has_many 可能没有任何在他们的方法不同,但他们在选择不同的你可以通过。例如,您可以通过在

has_and_belongs_to_many and has_many probably don't have anything different in their methods, but they differ in the options you can pass. For example, you can pass in

:dependent => :destroy

的has_many 的关联,但你不能把它传递给一个 has_​​and_belongs_to_many ,因为这将不是有意义的,因为它意味着许多-to-many关联;如果父记录被破坏,子记录仍然可以与其他记录相连,因此它们不应该也被破坏。

on the has_many association, but you can't pass it to a has_and_belongs_to_many, because that wouldn't make sense, since it implies a many-to-many association; if a parent record is destroyed, child records can still be connected with other records, so they shouldn't also be destroyed.

这篇关于的has_many:通过与has_and_belongs_to_many Rails中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆