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

后端 未结 3 1196
我寻月下人不归
我寻月下人不归 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:56

    Here's a trick I figured out. Don't assume it's intended behaviour though:

    class UserSubclass < User
    
      def self.model_name
        User.model_name
      end
    
    end
    

    Basically all models (that derive from ActiveModel) identify themselves, by default, based on the concrete class name. That's done through the class method #model_name (it returns an instance of ActiveModel::Name with self as the parameter. Overriding it to return a specific class puts Rails on the right track. This way you keep that logic in your model and out of your templates.

提交回复
热议问题