I''m using acts_as_tree to maintain a hierarchy of categories (each top-level category can contain multiple categories and each sub-category can contain multiple sub-categories, etc.). I want to be able to find all parents of a particular category. I realize that I can use @category.parent or even @category.parent.parent to move up in the hierarchy, but I need this to happen dynamically within the view because the hierarchy of each category may be different. Any ideas on how to handle this? Thanks, -Jason jasongilstrap-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Jason Gilstrap wrote:> I''m using acts_as_tree to maintain a hierarchy of categories (each > top-level category can contain multiple categories and each > sub-category can contain multiple sub-categories, etc.). > > I want to be able to find all parents of a particular category. I > realize that I can use @category.parent or even > @category.parent.parent to move up in the hierarchy, but I need this > to happen dynamically within the view because the hierarchy of each > category may be different. > > Any ideas on how to handle this?Untested, but: <% current = @category while parent = current.parent %> <%= render_parent parent %> <% current = parent end %> should do the trick... -- Alex
As soon as you want to retrieve ALL parents or children of a tree, you should model your data as nested set rather than parent-child, so that you can do it in one swoop of SQL instead of hitting the DB with one request per generation. See act_as_tree: http://api.rubyonrails.org/classes/ActiveRecord/Acts/NestedSet/ClassMethods.html There''s no direct method for all the parents, but it''s as simple as SELECT PARENT.* WHERE CHILD.LEFT IS BETWEEN PARENT.LEFT AND PARENT.RIGHT Xavier Jason Gilstrap wrote:> I''m using acts_as_tree to maintain a hierarchy of categories (each > top-level category can contain multiple categories and each > sub-category can contain multiple sub-categories, etc.). > > I want to be able to find all parents of a particular category. I > realize that I can use <at> category.parent or even > <at> category.parent.parent to move up in the hierarchy, but I need this > to happen dynamically within the view because the hierarchy of each > category may be different. > > Any ideas on how to handle this?
Francois Beausoleil
2005-Oct-20 15:41 UTC
Re: Re: Finding all parents of acts_as_tree relationship
Hi, 2005/10/20, Xavier Priour <xavier@priour.com>:> As soon as you want to retrieve ALL parents or children of a tree, you > should model your data as nested set rather than parent-child, so that > you can do it in one swoop of SQL instead of hitting the DB with one > request per generation.Aren't there all kinds of problems with acts_as_nested_set ? I seem to remember a recent thread on problems with it. I know I myself tried to use it at some point, but decided not to because I couldn't get it to work reliably. I can't remember what the problems were, unfortunately. Bye, François _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails