Rails ActiveRecord 关系 - 有很多且属于关联

发布于 2024-08-15 02:52:19 字数 379 浏览 4 评论 0原文

我创建了 3 个模型:

  • 文章:包含一篇文章
  • 标签:包含标签
  • ArticleTag:用于将多对一标签与文章关系关联起来。它包含一个tag_id 和一个article_id。

我遇到的问题是我对活动记录技术相当陌生,我不明白定义一切的正确方法。目前,我认为这是错误的,我有一个

ArticleTag
 belongs_to :article
 belongs_to :tag

“现在”,从这里我的想法是然后添加

  Article
   :has_many :tag

我不确定我是否正确地处理了这个问题。感谢您的帮助!

I've created 3 models:

  • Article: contains an article
  • Tag: contains tags
  • ArticleTag: meant for associating a many-to-one tags to article relationship. It contains a tag_id and an article_id.

The problem I'm having is I'm fairly new to the active record technology and I don't understand the proper way to define everything. Currently, which I think is wrong, is I have a

ArticleTag
 belongs_to :article
 belongs_to :tag

Now, from here my thought was to then add

  Article
   :has_many :tag

I'm not sure if im approaching this correctly at all. Thanks for the help!

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

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

发布评论

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

评论(3

七分※倦醒 2024-08-22 02:52:19

这取决于您是否想要连接模型。连接模型允许您针对两个其他模型之间的关联保存额外的信息。例如,也许您想记录文章被标记的时间戳。该信息将根据连接模型进行记录。

如果您不需要连接模型,则可以使用简单的 has_and_belongs_to_many 关联:

class Article < ActiveRecord::Base
  has_and_belongs_to_many :tags
end

class Tag < ActiveRecord::Base
  has_and_belongs_to_many :articles  
end

使用 Tagging 连接模型(这是一个比 ArticleTag< 更好的名称) /code>),它看起来像这样:

class Article < ActiveRecord::Base
  has_many :taggings
  has_many :tags, :through => :taggings
end

class Tag < ActiveRecord::Base
  has_many :taggings
  has_many :articles, :through => :taggings  
end

class Tagging < ActiveRecord::Base
  belongs_to :article
  belongs_to :tag
end

It depends whether you want a join model or not. A join model lets you hold extra information against the association between two other models. For example, perhaps you want to record a timestamp of when the article was tagged. That information would be recorded against the join model.

If you don't want a join model, then you could use a simple has_and_belongs_to_many association:

class Article < ActiveRecord::Base
  has_and_belongs_to_many :tags
end

class Tag < ActiveRecord::Base
  has_and_belongs_to_many :articles  
end

With a Tagging join model (which is a better name than ArticleTag), it would look like this:

class Article < ActiveRecord::Base
  has_many :taggings
  has_many :tags, :through => :taggings
end

class Tag < ActiveRecord::Base
  has_many :taggings
  has_many :articles, :through => :taggings  
end

class Tagging < ActiveRecord::Base
  belongs_to :article
  belongs_to :tag
end
梨涡 2024-08-22 02:52:19

当关系是单向时,您应该使用 has_many。一篇文章有​​很多标签,但标签也有很多文章,所以这不太正确。更好的选择可能是 has_and_belongs_to_many:一篇文章有​​许多标签,并且这些标签也引用文章。 Abelongs_toB表示A引用B; A has_one B 表示 B 引用 A。

以下是您可能会看到的关系的摘要:

Article
  has_and_belongs_to_many :tags   # An article has many tags, and those tags are on
                                  #  many articles.

  has_one                 :author # An article has one author.

  has_many                :images # Images only belong to one article.

  belongs_to              :blog   # This article belongs to one blog. (We don't know
                                  #  just from looking at this if a blog also has
                                  #  one article or many.)

You should use has_many when the relationship is one-way. An article has many tags, but tags also have many articles, so that's not quite right. A better choice might be has_and_belongs_to_many: an Article has many Tags, and those Tags also reference articles. A belongs_to B means that A references B; A has_one B means that B references A.

Here's a summary of the relationships you might see:

Article
  has_and_belongs_to_many :tags   # An article has many tags, and those tags are on
                                  #  many articles.

  has_one                 :author # An article has one author.

  has_many                :images # Images only belong to one article.

  belongs_to              :blog   # This article belongs to one blog. (We don't know
                                  #  just from looking at this if a blog also has
                                  #  one article or many.)
那伤。 2024-08-22 02:52:19

在我的脑海中,文章应该是:

has_many :article_tags
has_many :tags, :through => :article_tags

Off the top of my head, Article should be:

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