views:

41

answers:

2

Hello, I have two models on the Rails app that I am working on, Article and Profile. I want them to have separate sets of categories. Should I create two different category models? article_category and profile_category? There will be lots of redundant code if I do this.

+1  A: 

The question is, would both categories function the same way?

If they do, then I would use one model of categories, even though different names would be attributed different.

The trick is to DRY or "Don't Repeat Yourself". So if anything gets written twice, you can absolutely refactor that into a smaller code.

Trip
Okay, I think I will have booleans within the category model, profile_cat? and article_cat?.
kelp
Will any of them share the same category name?
Trip
No, they will not share the same category name.
kelp
Ah right, but you want the ability to pull down categories without pulling down the others? You can do what you mentioned, or set up the seperate names of them as a Constant array. :D And draw from the constant variable into a general Category model with shared functionality. I don't know too much about your project story, so its hard for me to say.
Trip
+1  A: 

If the models will be identical, then you should use a polymorphic relationship. See my response here: http://stackoverflow.com/questions/3498081/3499050#3499050

If the different category models will need additional logic, then I would use single table inheritance for this. You would have a parent Category class, and ArticleCategory and ProfileCategory would inherit from this. Basically all you will need to do is add a type field to categories and the two additional classes. This will eliminate redundant code and allow you to separate any model specific code into the appropriate class. Let me know if you need additional direction on this.

Beerlington