Should I create two different sets of “category” models? Ruby on the rails

I have two models in the Rails application I'm working on, articles and profiles. I want them to have separate categories. Should I create two models of different categories? article_category and profile_category? If I do this, there will be a lot of redundant code.

+4
source share
2 answers

The question is, will both categories work the same?

If so, then I would use one model of categories, although different names would be assigned to others.

The trick is DRY or Do Not Repeat Yourself. So if something is written twice, you can completely edit it into smaller code.

+2
source

If the models are identical, then you should use polymorphic relationships. See My answer here: Rails - How to set up a model that can belong to any of three different models

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

+2
source

All Articles