【发布时间】:2010-07-20 22:44:43
【问题描述】:
我正在开发一个类似 stackoverflow 的应用程序,其中的问题或文章只有一个标签。并且一个标签必须有一篇或多篇文章。
所以,我在 RoR 的迁移中这样做。我正在考虑哪种关系适合这两个表。在文章表中,应该使用“has_many”,在标签表中,应该使用“has_many”。 但我在想是否有必要在中间再添加一张桌子,比如....
所以,第一个是这样的:
class Article < ActiveRecord::Base
has_many :tags
end
class Tag < ActiveRecord::Base
has_many :articles
end
或类似的东西:
class Article < ActiveRecord::Base
has_many :articleTagList
has_many :tags, :through => : articleTagLists
end
class Tag < ActiveRecord::Base
has_many :articleTagList
has_many :articles, :through => :articleTagLists
end
class ArticleTagList < ActiveRecord::Base
belongs_to :article
belongs_to :tag
end
【问题讨论】:
-
我可以建议使用github.com/mbleigh/acts-as-taggable-on 吗?
标签: ruby-on-rails migration entity-relationship