Assume a polymorphic association, say 'business' and 'staff', both of which are 'hourable' (meaning they have hours assigned to them). What's the recommended approach to have the 'hour' model performs the same methods on the hours of either a business object or a staff object?
For a simple example, the 'hour' model might contain:
def self.find_hours_for_id(id)
Business.find( id ).hours
end
However, I may want to perform this same method on a Staff member, in which case the same method would instead call the equivalent of
Staff.find( id ).hours
Is it good or bad form to set the model name within the base model class:
class BusinessHour < Hour
@mymodel = "Business"
end
class Hour < ActiveRecord::Base
def self.find_hours_for_id(id)
@mymodel.constantize.find( id ).hours
end
end
Then from the controller, call:
BusinessHour.find_hours_for_id(id)
Is there a better solution?