Using Rails 3.2. My models are nested in such a way:
I think I had the same problem just now. I got an ActiveRecord::EagerLoadPolymorphicError
when trying to load associations. My solution was to put together a custom join statement which I've put into a scope for easier usage.
Untested, but this may get you going:
class Review < ActiveRecord::Base
scope :eagerly_loaded, -> {
joins('
INNER JOIN shops
ON (reviews.reviewable_type = "Shop" AND reviews.reviewable_id = shops.id)
INNER JOIN countries
ON (reviews.reviewable_type = "Country" reviews.reviewable_id = countries.id)
')
}
end
You then use @user.reviews.eagerly_loaded
. Make sure to use a matching .group()
-statement to only get the ones you need.