0

I've been trying to find a proper solution for this problem but didn't succeed. I know that we can't do select with include statement.

For example I have a model called Parent which have many children. I tried following things

1) When I tried this

Parent.includes(:children).select("parent.name, children.age")

Rails completely ignores the select clause.

2) Then I tried this

Parent.joins(:children).select("parent.name, children.age")

The select clause works but instead of returning a nested object it returns me a flat array of objects. So I have to again run a group by command on it to make it nested.

3) I found something called preload, but again not enough documentation for it.

I'm tired of finding a solution to this problem. Can someone point me in a right direction.

===================================================================

By nested object I meant I should be able to do things like

 @parents.each do |parent|
   puts parent.name
   parent.children.each do |child|
     puts child.age
   end
 end

I can achieve this with include but then it selects all attributes which are not needed.

4

0 回答 0