Is subclassing a User model really bad to do in Rails?

后端 未结 3 1199
我寻月下人不归
我寻月下人不归 2021-01-04 23:30

I am getting lots of push back from Rails because I have subclassed User into many different subclasses. In my application, not all users are equal. There\'s actually a lot

3条回答
  •  情话喂你
    2021-01-04 23:38

    Generally speaking, inheritance is discouraged in Ruby in favor of mixin behavior and delegation. Ruby and Rails can do that stuff but it tends to result in the push back you mentioned

    Your particular example sounds like a case for delegation: have a User class which belongs to an employee (or vice versa). The type-specific behavior of that employee (eg director, instructor, etc) is all in that specific employee class. Then the user will delegate how to handle specific scenarios to the employee that it's joined with

提交回复
热议问题