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

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -