Ruby on Rails Wednesday, July 2, 2014

Let's have model :parents and :children, where one parent can have many
children, and that I want to do something like this:

plist=Parent.joins(:children).select('*').where("parents.id=children.parent_id
and children.cfield=#{...}")

plist.each { |p| do_something_with(p,p.children) }

Now I learned from
http://guides.rubyonrails.org/active_record_querying.html , that this is
inefficient, due to the SELECT statements generated inside the block,
and that I should do "eager loading" instead. From my understanding of
the tutorial, I should replace 'joins' by 'includes':

plist=Parent.includes(:children).select('*').where("parents.id=children.parent_id
and children.cfield=#{...}")

However, this raises the exception that there would be no column
"children.cfield".

It seems that with 'includes', we can only query based on values of the
Parent table.

Is this correct?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f814f36645d4f47a87ebf77a0a8d3194%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment