# Eager loading with acts\_as\_tree

**URL:** <https://www.ruby-forum.com/t/eager-loading-with-acts-as-tree/68570>\
**Category:** Rails\
**Created:** [September 13, 2006, 6:07pm UTC](https://www.ruby-forum.com/t/eager-loading-with-acts-as-tree/68570 "2006-09-13T18:07:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Szymon\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@Szymon\_R](https://www.ruby-forum.com/u/Szymon_R)\
**Post date:** [September 13, 2006, 6:07pm UTC](https://www.ruby-forum.com/t/eager-loading-with-acts-as-tree/68570/1 "2006-09-13T18:07:47Z")

</div>

So I have a model that acts\_as\_tree and I run a method on a collection  
of all of these records. This method walks the tree and fetches the  
parent/children, etc. This results in many, many duplicate SQL queries  
to be executed. Is there a way to eagerly load all of these models?

Something along the lines of:  
Model.find(:all, :include =\> [:children, :parent])

If I execute the above statement, I get an exception:  
Mysql::Error: Column: ‘parent\_id’ in where clause is ambiguous

-Szymon

---

<div class="post-metadata">

**Author:** ![Szymon\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e480ec/32.png) [@Szymon\_R](https://www.ruby-forum.com/u/Szymon_R)\
**Post date:** [September 13, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/eager-loading-with-acts-as-tree/68570/2 "2006-09-13T20:28:05Z")

</div>

Szymon R. wrote:

> -Szymon

Well I have found out that when I just load the root nodes/parents, it  
automagically gets the children for me, but of course inside the  
ActiveRecord-results. If I dont pass a condition, eg… “@things =  
Things.find(:all)”, I will get all the children in @things and under  
“thing.children”. If I pass a condition like “parent\_id = 0” then  
@things just contains the root nodes, but the children are still  
accessible.

Not sure if this was helpful, or if it even lessens the amount of SQL  
Queries, but It produces more logical code.
