开发者

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

开发者 https://www.devze.com 2023-01-06 13:36 出处:网络
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_hou

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消