Skip to main content

Should Categories be it's own resource or model attribute?

Databases • Asked by Ariff Munshi
Hey guys, 

I have a question on database design. Say I have a Post model that has/belongs to a category (out of about 20 categories or so). I am thinking of two options that have their own merits.

1. Category as own resource with Post belong_to it
- this allows listing of all categories and posts under a category easy
- however, entries will most probably be done once at the start and then left to be

hence the other option

2. Categories as an attribute of Post
- scoping Post by categories is trivial still
- categories can be picked from a constant list

Would love to hear any of your comments on this how would you go about doing it.

Cheers!


Hey Chris, just wondering, have you designed the categories here in the forums to be its own Category model or a column list?

I setup my category as a model.  

Old post I know but it's quite a general question so will add my thoughts.

My decision making has always come down to whether there will only ever be one category, or potential multiple categories. For the singular, you can probably just add a column in your model, but for multiple entries, you might want to create a seperate category model, and then reference the two via a joins table.

This approach is very common for tags for example, but to be honest, I've started using it in many areas that can have multiple entries. e.g. flavours in coffee or different gin brands served by a bar.

At the end of the day, it's all just a type of tagging.


Hey Simon,

Thanks for your answer! Never actually thought of it that way. Come to think of it, it makes the most sense. Cheers!


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.