Friday, 9 August 2013

Eager loadings for single object?

Eager loadings for single object?

I have a controller that find 1 record (its uniq)
class CollectionsController < ApplicationController
def show
@collection = Collection.find_by_active(true)
end
end
This '@collection' have many :items, which has many :photos
So, in view it look like this:
- @collection.items.each do |item|
= image_tag item.find_active_photo.image_url(:small)
And it will reproduce bunch of SQL queries, for example if item has 4
photos, it will be 6 queries:
Collection Load
CollectionItem Load
Photo Load (4 times)
How I can reduce count of this N+1 queries?

No comments:

Post a Comment