需要模型建议 - 帖子、类别、标签
我想创建一个类似博客的东西。
帖子必须属于一个类别。每个类别都有许多标签(或子类别?)。帖子可以有标签(可选)。
这是最好的方法吗?
Category
has_many :tags
has many :posts
Tag
belongs_to :category
has_and_belongs_to_many :posts
Post
belongs_to category
has_and_belongs_to_many :tags
I want to create something like a blog.
A Post must belong to a Category. Each Category has many Tags (or subcategories?). A post can have Tags (optionally).
Is this the best way to do it?
Category
has_many :tags
has many :posts
Tag
belongs_to :category
has_and_belongs_to_many :posts
Post
belongs_to category
has_and_belongs_to_many :tags
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您给出的模型架构几乎是您所写句子的精确翻译,是的,这应该是最好的方法!
对于您可能想要使用插件的标签,请参阅:按排名标记插件列表< /a>
Since the schema of models you gave is almost the exact translation of the sentence you wrote, yes it should be the best way !
For the tag you maybe would like to use a plugin, see : tagging plugins list order by ranking
我会使用 多态关联 代替。这将使您能够很好地控制标签集(标签数量、可用标签等),但会减慢基于标签的搜索速度。
另一种方法是使用您选择的数据库的全文搜索功能。
这将为您提供基于标签的快速搜索,但标签设置操作速度较慢。
I would use polymorhic associations instead. This will give you good control over the tag set (number of tags, available tags, etc) but will slow down tag based search.
Another aproach is to use the full text search capabilities from your database of choice.
This will give you a fast tag based search but slow tag set operations.