I have 4 models, let's say:
class Photo < ActiveRecord::Base
belongs_to :photoable, polymorphic: true
end
class User < ActiveRecord::Base
has_one :photo, as: :photoable
end
class Company < ActiveRecord::Base
has_one :photo, as: :photoable
has_many :products
end
class Products < ActiveRecord::Base
belongs_to :company
end
So, the query Photo.all.includes(:photoable)
works.
But if I use Photo.all.includes(photoable: :products)
only works if all loaded photos belongs to Company. If the relation contains photos of users and companies, this error is raised:
ActiveRecord::ConfigurationError (Association named 'products' was not found; perhaps you misspelled it?):
This occurs because user hasn't relationship with products.
Is there any way to eager load users and companies with products for a relation of photos?
EDIT:
This question isn't duplicated of Eager load polymorphic. As I commented below, in this question I want to do eager load for polymorphic associations which has different associations(one has products and the other don't). In that question, the OP uses wrong names for table names.