Exam is a parent of SatTest and ActTest like so
class Exam < ActiveRecord::Base
self.inheritance_column = :test_type
def self.find_sti_class(type_name)
type_name = self.name
super
end
end
class ActTest < Exam
def self.sti_name
'ACT'
end
def some_method
end
end
class SatTest < Exam
def self.sti_name
'SAT'
end
def some_method
end
end
When I query Exam.find(1)
it returns an instance of Exam. As a result, when I call some_method
on this object, it calls returns undefined method some_method
instead of calling the method on it's subclass.
What is the best way to send the method down to its subclass without having to requery again? I know I could do this, but it seems pretty hacky
class Exam < ActiveRecord::Base
def some_method
if self.type == "SAT"
SatTest.find(self.id).some_method
elsif self.type == "ACT"
ActTest.find(self.id).some_method
end
end
end
UPDATE In regards to the type field, I have edited the models above (it is renamed to test_type in my app). I'm using rails 4.2.1.