# How best to extend acts\_as\_tree?

**URL:** <https://www.ruby-forum.com/t/how-best-to-extend-acts-as-tree/137400>\
**Category:** Rails\
**Created:** [May 12, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/how-best-to-extend-acts-as-tree/137400 "2008-05-12T20:56:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [May 12, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/how-best-to-extend-acts-as-tree/137400/1 "2008-05-12T20:56:41Z")

</div>

I’ve got a bunch of tables that will act\_as\_tree. I don’t see a method  
(maybe I’m missing it) to “get myself and my children”? So I wanted to  
add it to my model, but I have so many acts\_as\_tree models I was trying  
to figure out a way to write my self\_and\_children method once, but I’m  
unfamiliar with extending the existing code in this way. Can somebody  
point me in the right direction? Thanks! (This code is for a rapid  
prototype, so scale and performance are not crucial issues, I’m just  
can’t bring myself to cut and paste the same method over and over  
again.)

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [May 12, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/how-best-to-extend-acts-as-tree/137400/2 "2008-05-12T21:02:02Z")

</div>

I’ve just realized that if an object’s parent\_id is set equal to itself,  
then it will include itself among it’s children. Perhaps that’s all I  
need, at least for my purposes right now. Sorry for the interruption.

Duane M. wrote:

> I’ve got a bunch of tables that will act\_as\_tree. I don’t see a method  
> (maybe I’m missing it) to “get myself and my children”? So I wanted to  
> add it to my model, but I have so many acts\_as\_tree models I was trying  
> to figure out a way to write my self\_and\_children method once, but I’m  
> unfamiliar with extending the existing code in this way. Can somebody  
> point me in the right direction? Thanks! (This code is for a rapid  
> prototype, so scale and performance are not crucial issues, I’m just  
> can’t bring myself to cut and paste the same method over and over  
> again.)

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [May 13, 2008, 12:10am UTC](https://www.ruby-forum.com/t/how-best-to-extend-acts-as-tree/137400/3 "2008-05-13T00:10:46Z")

</div>

If you want the object and it’s children all in a 1D array:

def self\_and\_children  
[self] + children  
end
