# Acts\_as\_tree

**URL:** <https://www.ruby-forum.com/t/acts-as-tree/99117>\
**Category:** Ruby\
**Created:** [July 20, 2006, 11:58am UTC](https://www.ruby-forum.com/t/acts-as-tree/99117 "2006-07-20T11:58:22Z")\
**Posts on this page:** 3\
**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:58am UTC](https://www.ruby-forum.com/t/acts-as-tree/99117/1 "2006-07-20T11:58:22Z")

</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, 2:48pm UTC](https://www.ruby-forum.com/t/acts-as-tree/99117/2 "2006-07-20T14:48:10Z")

</div>

On Jul 20, 2006, at 5:55 AM, AN@S wrote:  
[snip: ruby on rails problem]

As is often said on this list:

Rails questions go on the Rails mailing list.

[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<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, 3:57pm UTC](https://www.ruby-forum.com/t/acts-as-tree/99117/3 "2006-07-20T15:57:13Z")

</div>

AN@S wrote:

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

Please ask rails questions on the rails mailing list.

[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
James B.

[http://www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[Ruby Code & Style](http://www.artima.com/rubycs/) - The Journal By & For Rubyists  
[http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
[http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[http://www.30secondrule.com](http://www.30secondrule.com) - Building Better Tools
