Let’s say I have the following models:
Class Wishlist
belongs_to :user # User class is irrelevant here
has_many :inclusions
has_many :products, :through => :inclusions
end
Class Product
has_many :inclusions
has_many :wishlists, :through => :inclusions
end
Class Inclusion
belongs_to :product
belongs_to :wishlist
end
inclusions/index.html.erb
<%= render @inclusions %>
inclusions/_inclusion.html.erb
<%= "#{ inclusion.quantity } #{ inclusion.product.name } #{ inclusion.wishlist.user.name }"%>
This example is trivial, but the point is: the amount of database queries is overwhelming. For every instance of _inclusion.html.erb, at least three new queries seem to be created.
Is there a way to prefetch this information beforehand, maybe through a JOIN command?
Maybe this can help:
Read about eager loading.