views:

54

answers:

1

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?

A: 

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.

Winfield
I don't need to, but I chose to because all the methods would otherwise be duplicated in both controllers and models.
99miles
An alternative might be to flatten your model inheritance and use a single controller. What is the value of having the two subtype of models? You can probably get the same behavior without the complexity of using inheritance.
Winfield