Ruby on Rails 3 unidirectional inheritance or multiple table inheritance?

I am working on Ruby on Rails 3. Already, I have implemented authentication and authorization based on Devise and Cancan. As part of this, I implemented a Member object, which can have at least two different roles - User and Company. Physically, I would like to represent these roles as two derived classes from Member, such as User and Company.

Should I use unidirectional inheritance or multiple table inheritance. In the case of multiple table inheritance, which plugin should I use?

+4
source share
1 answer

I know this is not the answer to your question, but I think you should reconsider using something other than inheritance.

Inheritance is an excellent OO mechanism, but is very often used. More often than not, you can work better with an association and some Strategy Pattern than with inheritance.

Activerecords are much more compatible with this design than inheritance, and it also respects the principle of shared responsibility much more, which means it is easier to test in isolation.

If you want to go with a strategy template in your activerecord model, there might be a way to polymorphic association (this even allows you to have stateful strategies)

+4
source

Source: https://habr.com/ru/post/1397384/


All Articles