# Acts\_as\_tree help

**URL:** <https://www.ruby-forum.com/t/acts-as-tree-help/136156>\
**Category:** Rails\
**Created:** [April 29, 2008, 1:22pm UTC](https://www.ruby-forum.com/t/acts-as-tree-help/136156 "2008-04-29T13:22:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [April 29, 2008, 1:22pm UTC](https://www.ruby-forum.com/t/acts-as-tree-help/136156/1 "2008-04-29T13:22:18Z")

</div>

Hi all…  
I created a table having a tree structure using acts\_as\_tree. Now I  
need to access all the parents, grandparents etc upto the root of a  
particular node. Also  
all the children upto leaf node.  
I used particular\_node\_id.children() method to access the immediate  
children  
and particular\_node\_id.ancestors() to access the immediate parents of a  
node.

Please help me to find all the parents upto root and all the children  
upto leaf of a particular node.

Thanks  
Regards  
Suneeta

---

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [April 29, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/acts-as-tree-help/136156/2 "2008-04-29T17:21:47Z")

</div>

I used this code to get all the ancestors for a given node:

def ancestors(list=[])  
if parent.nil?  
list \<\< parent  
else  
list \<\< parent  
parent.ancestors(list)  
end  
list.compact  
end

---

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [April 29, 2008, 5:52pm UTC](https://www.ruby-forum.com/t/acts-as-tree-help/136156/3 "2008-04-29T17:52:03Z")

</div>

On Apr 29, 7:22 am, Suneeta Km [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Please help me to find all the parents upto root and all the children  
> upto leaf of a particular node.

You should use act\_as\_nested\_set or better\_nested\_set. They are  
better optimized for this, and it has built-in methods to accomplish  
what you are asking for.
