# Acts as tree

**URL:** <https://www.ruby-forum.com/t/acts-as-tree/122821>\
**Category:** Rails\
**Created:** [December 12, 2007, 4:18pm UTC](https://www.ruby-forum.com/t/acts-as-tree/122821 "2007-12-12T16:18:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![venu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@venu](https://www.ruby-forum.com/u/venu)\
**Post date:** [December 12, 2007, 4:18pm UTC](https://www.ruby-forum.com/t/acts-as-tree/122821/1 "2007-12-12T16:18:42Z")

</div>

Hi,

Can somebody give me what could be the structure and content of  
locations table, if that is to be defined as acts\_as\_tree?

Your help is appreciated and thanks in advance.

class Location \< ActiveRecord::Base  
acts\_as\_tree :order =\> “location\_name”  
end

---

<div class="post-metadata">

**Author:** ![venu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@venu](https://www.ruby-forum.com/u/venu)\
**Post date:** [December 12, 2007, 4:33pm UTC](https://www.ruby-forum.com/t/acts-as-tree/122821/2 "2007-12-12T16:33:22Z")

</div>

Venu Vallayil wrote:

> Hi,
> 
> Can somebody give me what could be the structure and content of  
> locations table, if that is to be defined as acts\_as\_tree?
> 
> Your help is appreciated and thanks in advance.
> 
> class Location \< ActiveRecord::Base  
> acts\_as\_tree :order =\> “location\_name”  
> end

all you need is a

“parent\_id” column in the table/model. dat’s it. you’ll gain the  
following methods:

location.ancestors # all parents of object  
location.root # returns the first first parent in the leaf  
location.self\_and\_siblings # the same generation of objects  
location.siblings # same generation, excluding the location object  
location.parent # the direct parent of the location object  
location.children # the direct children of the location object or an  
empty array if no children exist

hth…

[http://wiki.rubyonrails.org/rails/pages/ActsAsTree](http://wiki.rubyonrails.org/rails/pages/ActsAsTree)

---

<div class="post-metadata">

**Author:** ![venu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@venu](https://www.ruby-forum.com/u/venu)\
**Post date:** [December 12, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/acts-as-tree/122821/3 "2007-12-12T16:35:49Z")

</div>

> Can somebody give me what could be the structure and content of  
> locations table, if that is to be defined as acts\_as\_tree?

(( missed the question; the table should be ))

# table locations

id int auto\_increment not null  
parent\_id int not null  
location\_name text\_or\_whatever not null  
primary key(id) …  
(you could also index the parent\_id column for added performace sql  
wise)

that should suffice 😉  
enjoy

---

<div class="post-metadata">

**Author:** ![venu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@venu](https://www.ruby-forum.com/u/venu)\
**Post date:** [December 12, 2007, 4:56pm UTC](https://www.ruby-forum.com/t/acts-as-tree/122821/4 "2007-12-12T16:56:49Z")

</div>

Shai R. wrote:

> > Can somebody give me what could be the structure and content of  
> > locations table, if that is to be defined as acts\_as\_tree?
> 
> (( missed the question; the table should be ))
> 
> # table locations
> 
> id int auto\_increment not null  
> parent\_id int not null  
> location\_name text\_or\_whatever not null  
> primary key(id) …  
> (you could also index the parent\_id column for added performace sql  
> wise)
> 
> that should suffice 😉  
> enjoy

Shai…let me try with your input! I am a “newbie” in ROR

Thanks  
Venu
