# Using :include "recursively" (including the children of the

**URL:** <https://www.ruby-forum.com/t/using-include-recursively-including-the-children-of-the/50487>\
**Category:** Rails\
**Created:** [December 16, 2005, 9:16pm UTC](https://www.ruby-forum.com/t/using-include-recursively-including-the-children-of-the/50487 "2005-12-16T21:16:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSISO-8859-1SQSIvS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@SSSISO-8859-1SQSIvS](https://www.ruby-forum.com/u/SSSISO-8859-1SQSIvS)\
**Post date:** [December 16, 2005, 9:16pm UTC](https://www.ruby-forum.com/t/using-include-recursively-including-the-children-of-the/50487/1 "2005-12-16T21:16:47Z")

</div>

Hi,

Let’s say I have the following hierarchy of tables:

Group \<- parent  
Location \<- child of Group  
School \<- child of Location  
Phone \<- child of School

Then, if I want to do this:

Location.find(…someparms…, :include =\> [:schools, :group, :phones])

I get an association error (Association was not found). I guess it  
happens because AR is looking in the Location model for the Phones  
association, which only exists in the School model (I also tried  
singular, :phone).

Is there any way I can load all Phone records at once? I wouldn’t mind  
using custom SQL if that’s the only solution.

Thanks in advance!

Ivan V.

PS: Merrrrry christmas!

---

<div class="post-metadata">

**Author:** ![SSSISO-8859-1SQSIvS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@SSSISO-8859-1SQSIvS](https://www.ruby-forum.com/u/SSSISO-8859-1SQSIvS)\
**Post date:** [December 16, 2005, 9:22pm UTC](https://www.ruby-forum.com/t/using-include-recursively-including-the-children-of-the/50487/2 "2005-12-16T21:22:49Z")

</div>

On Fri, Dec 16, 2005 at 02:14:57PM -0600, Iv?n Vega R. wrote:

> Is there any way I can load all Phone records at once? I wouldn’t mind  
> using custom SQL if that’s the only solution.

There is no way to do that using the eager loading implementation. It  
would  
require custom SQL.

marcel

---

<div class="post-metadata">

**Author:** ![SSSISO-8859-1SQSIvS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@SSSISO-8859-1SQSIvS](https://www.ruby-forum.com/u/SSSISO-8859-1SQSIvS)\
**Post date:** [December 16, 2005, 9:41pm UTC](https://www.ruby-forum.com/t/using-include-recursively-including-the-children-of-the/50487/3 "2005-12-16T21:41:03Z")

</div>

Marcel Molina Jr. wrote:

> 

Would it be possible to load the phones for the object:

@locs = Location.find(…someparms…, :include =\> [:schools, :group])

and then something like:

@locs.schools.phones.load\_all (of course this method doesn’t exist, but  
is there something similar?)

Or could I “find” all the phones that belong to certain schools and then  
assign those to the specific @locs.schools object?

Thanks!

Ivan V.

---

<div class="post-metadata">

**Author:** ![SSSISO-8859-1SQSIvS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@SSSISO-8859-1SQSIvS](https://www.ruby-forum.com/u/SSSISO-8859-1SQSIvS)\
**Post date:** [December 20, 2005, 11:17am UTC](https://www.ruby-forum.com/t/using-include-recursively-including-the-children-of-the/50487/4 "2005-12-20T11:17:50Z")

</div>

If it’s just for the location, then you could include a redundant  
foreign  
key (has\_many) to the phones. Then you could use Location.find(  
…someparams :include =\> [:phones])

Alternativley a method in the Locations model could look something like  
for  
the current location

def self.get\_phones  
Phone.find\_by\_sql(  
“SELECT phones.\* FROM phones p, locations l, schools s  
WHERE l.id = #{id} AND s.location\_id = l.id AND p.school\_id =  
s.id”)  
end

In which case you could use @some\_location.get\_phones

going a bit further than that it might be possible to use something like

@phone\_numbers = Location.find( :all ).collect { |loc|  
loc.get\_phones}  
@phone\_numbers.flatten!

I haven’t tested andy of this tho… Hope it works 🙂
