Eager loading with acts_as_tree

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

Szymon Rozga wrote:

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

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.