开发者

Need advice with models - Post, Category, Tags

开发者 https://www.devze.com 2023-03-13 07:37 出处:网络
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).

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?

Categ开发者_开发知识库ory
 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消