Need advice with models - Post, Category, 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?
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.
精彩评论