简体   繁体   中英

Controller inheritance in Ruby on Rails — model and controller access in parent class

I am using single table inheritance in StudentHours and TeacherHours, which have a parent Hours.

The model code is mostly in hour.rb, and very little in student_hour.rb and teacher_hour.rb

Now I have realized that most of the controller code is duplicate as well, so I've created a hours_controller to be the parent of students_controller and teachers_controller. Because the hours_controller instantiates model objects such as TeacherHours.new I've created a accessor in the child classes such as:

def MyModel
 @mymodel = "TeacherHours"
end

... then the hours_controller simply calls MyModel.new

Occasionally there are other models that are referenced and sometimes I even call 'render' on a view so I made accessors in the child classes for those too.

I can't find example of others doing this. So, is this bad for any reason, or is there a better way?

Why do you need to use inheritance for the behavior of these models?

This sounds like unnecessary inheritance, both in the models and the controllers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM