ruby on rails - Eager loadings for single object? -
i have controller find 1 record (its uniq)
class collectionscontroller < applicationcontroller def show @collection = collection.find_by_active(true) end end
this '@collection' have many :items, has many :photos
so, in view this:
- @collection.items.each |item| = image_tag item.find_active_photo.image_url(:small)
and reproduce bunch of sql queries, example if item has 4 photos, 6 queries:
collection load collectionitem load photo load (4 times)
how can reduce count of n+1 queries?
use eager loading
@collection = collection.find_by_active(true, {:include =>{:items => :photos})
Comments
Post a Comment