I am new to Ruby and Rails so bear with me please. I have created a very simple blog application with both posts and comments. Everything works great. My next question regarding adding categories. I am wondering the best way to do this. As I can’t see too far in front of me yet when it comes to Rails I thought I would ask.
To be clear, I would like that a single post can have multiple categories and a category can have multiple posts.
Is the best way to do this to create a ‘categories’ table and then use the posts and categories models to do has_many :posts, has_many :categories? Would I also then set the routes.rb such that posts are embedded under categories?
Or is there an easier way by simply adding a category column to the existing posts table? (in which case I would imagine having multiple categories would be difficult).
Since you want a many-to-many relationship between categories and posts, I would (and do) use a tagging plugin such as ActsAsTaggableOn, where you can define what a “tag” is referred to, and call it “category.” Simple and effective. In your form, you can limit the possible values for a “tag” with a whitelist (like, “coding,” “recipes,” “vacation”) for your different categories.