# Models
class Animal < ActiveRecord::Base
belongs_to :zoo
end
class Dog < Animal
belongs_to :leg
end
class Snake < Animal
end
class Leg < ActiveRecord::Base
end
class Zoo
has_many :animals
end
# Test, which fails with
# Association named 'leg' was not found on Animal; perhaps you misspelled it?
Zoo.first.animals.
.where(:type => 'Dog')
.includes(:leg)
In this example, Rails can not know the specific type of the objects queried (it would have to analyze the where
statement for that, which it does not appear to do). Therefore, it fails, as the association is not defined on the generic model Animal
, but on the Dog
model.
Is there a way of specifying the type of the objects about to be retrieved, so that the example works?