# Acts\_as\_tree

**URL:** <https://www.ruby-forum.com/t/acts-as-tree/99116>\
**Category:** Rails\
**Created:** [July 20, 2006, 11:52am UTC](https://www.ruby-forum.com/t/acts-as-tree/99116 "2006-07-20T11:52:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [July 20, 2006, 11:52am UTC](https://www.ruby-forum.com/t/acts-as-tree/99116/1 "2006-07-20T11:52:01Z")

</div>

Hello,  
I’m trying to create categories that have their own subcategories, and  
each  
subcategory may have its own subcategories, … and so on.

After some reading, I found out that the best way for creating such a  
thing  
is to use acts\_as\_tree, but I didn’t find any tutorial or article that  
explain this in a clear way. After a lot of work I was finally able to  
create categories and subcategories using the following code explained  
in  
(Agile Web D. With Rails) book:

* * *

## root = Category.create(:name =\> “Books”) fiction = root.children.create(:name =\> “Fiction”) non\_fiction = root.children.create(:name =\> “Non Fiction”) non\_fiction.children.create(:name =\> “Computers”) non\_fiction.children.create(:name =\> “Science”) non\_fiction.children.create(:name =\> “Art History”) fiction.children.create(:name =\> “Mystery”) fiction.children.create(:name =\> “Romance”) fiction.children.create(:name =\> “Science Fiction”)

And here is the database scheme:

* * *

## create table categories ( id int not null auto\_increment, name varchar(100) not null, parent\_id int, constraint fk\_category foreign key (parent\_id) references categories(id), primary key (id) );

## in the model:

## class Category \< ActiveRecord::Base acts\_as\_tree :order =\> “name” end

OK, my problem now is how to view something! Any category name, or any  
subcategory or a list of categories, I tried a lot of things, like (in  
the  
view):

* * *

\<% for category in @categories %\> - \<%= link\_to h(category.title), :action =\> 'edit', :id =\> category %\> \<%= find\_all\_subcategories(category) %\> 
\<% end %\>
----------------------------------------------------------

But I ALWAYS and whatever I try I got this error message:

"You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.each

Extracted source (around line #2): \<% for category in @categories %\> "

What do you think the problem is?  
If you know some tutorial that fully explain how to use acts\_as\_tree to  
add  
and show categories that’ll be great

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [July 20, 2006, 12:30pm UTC](https://www.ruby-forum.com/t/acts-as-tree/99116/2 "2006-07-20T12:30:38Z")

</div>

AN@S,

Could you paste your controller code?

- Jean-Etienne

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [July 20, 2006, 1:23pm UTC](https://www.ruby-forum.com/t/acts-as-tree/99116/3 "2006-07-20T13:23:19Z")

</div>

Jean-Etienne D. wrote:

> AN@S,
> 
> Could you paste your controller code?
> 
> - Jean-Etienne

In the controller:

def create\_categories  
root = Category.create(:name =\> “Books”)  
fiction = root.children.create(:name =\> “Fiction”)  
non\_fiction = root.children.create(:name =\> “Non Fiction”)  
non\_fiction.children.create(:name =\> “Computers”)  
non\_fiction.children.create(:name =\> “Science”)  
non\_fiction.children.create(:name =\> “Art History”)  
fiction.children.create(:name =\> “Mystery”)  
fiction.children.create(:name =\> “Romance”)  
fiction.children.create(:name =\> “Science Fiction”)  
end

This works and create the categories in the database,

def show\_children  
display\_children(root) #this should show Fiction and Non Fiction  
end

I’m really lost, I just want to know how can I show something from  
categoreis table in the view

Regards,

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [September 25, 2007, 11:02pm UTC](https://www.ruby-forum.com/t/acts-as-tree/99116/4 "2007-09-25T23:02:36Z")

</div>

On 4/30/07, Eric Harris-Braun [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> For my app, I needed to modify acts\_as\_tree (ActiveRecord::Acts::Tree)  
> such that deleting a node would orphan sub-nodes, rather than deleting  
> them.

If you have orphaned nodes, how do you know which one is root?

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [July 20, 2006, 1:33pm UTC](https://www.ruby-forum.com/t/acts-as-tree/99116/5 "2006-07-20T13:33:14Z")

</div>

AN@S wrote:

> Jean-Etienne D. wrote:
> 
> > AN@S,
> > 
> > Could you paste your controller code?
> > 
> > - Jean-Etienne
> 
> In the controller:
> 
> def create\_categories  
> root = Category.create(:name =\> “Books”)  
> fiction = root.children.create(:name =\> “Fiction”)  
> non\_fiction = root.children.create(:name =\> “Non Fiction”)  
> non\_fiction.children.create(:name =\> “Computers”)  
> non\_fiction.children.create(:name =\> “Science”)  
> non\_fiction.children.create(:name =\> “Art History”)  
> fiction.children.create(:name =\> “Mystery”)  
> fiction.children.create(:name =\> “Romance”)  
> fiction.children.create(:name =\> “Science Fiction”)  
> end
> 
> This works and create the categories in the database,
> 
> def show\_children  
> display\_children(root) #this should show Fiction and Non Fiction  
> end
> 
> I’m really lost, I just want to know how can I show something from  
> categoreis table in the view
> 
> Regards,

So, if u have one action which creates categories (code you paste), you  
can have another action in your controller to display children of a  
given cat:

- controller:  
def view  
@category = Category.find(params[:id])  
end

- view (view.rhtml)  
\<%= h(@category.name) %\>  
  
\<% @category.children.each do |c| -%\>  
Child: \<%= h(@c.name) %\>  
  
\<% end -%\>

call ur controller action with an id as param.

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [September 25, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/acts-as-tree/99116/6 "2007-09-25T23:07:38Z")

</div>

> > For my app, I needed to modify acts\_as\_tree (ActiveRecord::Acts::Tree)  
> > such that deleting a node would orphan sub-nodes, rather than deleting  
> > them.
> 
> If you have orphaned nodes, how do you know which one is root?

The whole point is that I want to be able to represent multiple trees,  
not just one. There already is a method “roots” that works just fine  
when there are such orphaned nodes. The method “root” ceases really  
to have meaning, but that’s fine.

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [September 10, 2008, 4:36pm UTC](https://www.ruby-forum.com/t/acts-as-tree/99116/7 "2008-09-10T16:36:23Z")

</div>

On Sep 8, 11:04 pm, “Carlos Henrique Palhares” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> where I can add a patch to acts\_as\_tree plugin?

I’m not positive, but I think you’d fork the repo here:

> **[GitHub - rails/acts\_as\_tree: NOTICE: official repository moved to...](https://github.com/rails/acts_as_tree/tree/master)**
>
> master

…make a patch according to the basic idea behind these guidelines:

[http://rails.lighthouseapp.com/projects/8994/sending-patches](http://rails.lighthouseapp.com/projects/8994/sending-patches)

…and create a ticket with your patch here:

[http://rails.lighthouseapp.com/projects/8995-rails-plugins](http://rails.lighthouseapp.com/projects/8995-rails-plugins)

- Trevor
