I have the following models:
has_many pictures, picture
Variant has value price and I am trying to find the price of all pictures of selected (e.g. for user id 15) current user.
First I tried like this:
def value(user) total = 0 user.pictures.each do |picture| picture.variants.each do |variant| total += variant.price end end total.to_f end
How could I improve this code so that I can avoid the N+1 problem?