Search code examples
ruby-on-railsclass-design

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?

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

Solution

  • 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