ruby on rails 3 - Using includes with AREL functions -


i have following classes in application:

class prompt   has_many :entries end  class entry   belongs_to :prompt   belongs_to :user    def self.approved     where("is_approved")   end end  class user   has_many :entries end 

and want display table of "approved" entries given prompt , users belong_to. generate list following query:

prompt = prompt.find(prompt_id, :include => {:entries => :user})

but when run following loop, makes query each user rather using prefetched users

prompt.entries.approved.each |entry|    puts entry.user.id end 

how rewrite doesn't query each iteration of loop?

it query each user because entries.approved calling query *where('is_approved')* each entry. find statement merely pulling of prompts , creating objects access child attributes. think need statement selects of entries have attribute 'is_approved' , run through loop printing ids.

maybe try @entries = entry.where(is_approved: true).includes(entries: user) )

then

entries.user.each |user| puts user.id end

hope thats helps.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -