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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
skurmedel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-13 18:14 UTC
Re: Eager loading with acts_as_tree
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 > > -SzymonWell 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Apparently Analagous Threads
- eager loading parents and children (acts_as_tree)
- Using set_primary_key breaks acts_as_tree with non-integer column
- Many-To-Many w/acts_as_tree?
- counter_cache is not looking for children_count with acts_as_tree
- acts_as_tree, acts_as_list and is invincible (help!)