Acts as tree

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

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

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 :wink:
enjoy

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 :wink:
enjoy

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

Thanks
Venu