# Getting a a list of acts\_as\_tree items with hierarchy

**URL:** <https://www.ruby-forum.com/t/getting-a-a-list-of-acts-as-tree-items-with-hierarchy/144577>\
**Category:** Rails\
**Created:** [July 31, 2008, 6:11am UTC](https://www.ruby-forum.com/t/getting-a-a-list-of-acts-as-tree-items-with-hierarchy/144577 "2008-07-31T06:11:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joshua-asem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@joshua-asem](https://www.ruby-forum.com/u/joshua-asem)\
**Post date:** [July 31, 2008, 6:11am UTC](https://www.ruby-forum.com/t/getting-a-a-list-of-acts-as-tree-items-with-hierarchy/144577/1 "2008-07-31T06:11:31Z")

</div>

I’m using acts\_as\_tree to nest categories n-levels deep and am wondering  
if anyone out there in Ruby-land knows the best, least expensive way to  
start with top level categories and then loop recursively through each  
category going as many levels deep as needed, and then moving to the  
next top level category, etc.

Thanks,  
Josh

---

<div class="post-metadata">

**Author:** ![joshua-asem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e5b9ba/32.png) [@joshua-asem](https://www.ruby-forum.com/u/joshua-asem)\
**Post date:** [July 31, 2008, 6:56am UTC](https://www.ruby-forum.com/t/getting-a-a-list-of-acts-as-tree-items-with-hierarchy/144577/2 "2008-07-31T06:56:26Z")

</div>

I’ve added a method in my Forum model code just for this:

def descendants  
children.map { |f| !f.children.empty? ? f.children + [f]:  
f }.flatten  
end

def self.find\_all\_without\_parent  
find\_all\_by\_parent\_id(nil)  
end

It finds all the descendants of a given forum, you could just cycle  
through all top-level forums using the Forum.find\_all\_without\_parent  
method and just query about the descendants of each item. I believe my  
solution will give you a flat version of them however.
