# Having trouble listing tiers of categories

**URL:** <https://www.ruby-forum.com/t/having-trouble-listing-tiers-of-categories/63668>\
**Category:** Rails\
**Created:** [June 17, 2006, 7:01am UTC](https://www.ruby-forum.com/t/having-trouble-listing-tiers-of-categories/63668 "2006-06-17T07:01:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [June 17, 2006, 7:01am UTC](https://www.ruby-forum.com/t/having-trouble-listing-tiers-of-categories/63668/1 "2006-06-17T07:01:16Z")

</div>

I have categories and sub-categories in a table called “categories”.  
Every row has a “parent” field to note if the entry is the sub-category  
of another (never goes deeper than 1 level).

So there might be something like this:

id | name | parent  
1 Dog 0  
2 Pug 1  
3 Siamese 5  
4 Shih Tzu 1  
5 Cat 0  
6 Wiener Dog 1  
7 Persian 5

I’d like to list it like  
Dog  
–Pug  
–Shih Tzu  
–Wiener Dog

Cat  
–Siamese  
–Persian

Right now it’s only listing them in one long list. But I’d like to list  
each parent category and have the subcats listed under them.

Any ideas?

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [June 17, 2006, 7:16am UTC](https://www.ruby-forum.com/t/having-trouble-listing-tiers-of-categories/63668/2 "2006-06-17T07:16:38Z")

</div>

Josh P. wrote:

> 4 Shih Tzu 1  
> Cat  
> –Siamese  
> –Persian
> 
> Right now it’s only listing them in one long list. But I’d like to list  
> each parent category and have the subcats listed under them.
> 
> Any ideas?

If you can I would recommend redoing your db and models so that you have  
categories and sub\_categories. That would make this really easy. All  
you would need to do is setup a category has\_many :sub\_categories and  
rails would basically do the rest for you.  
For instance if you setup the association then you could put this in a  
view (set @categories = Category.find(:all) in your controller)  
\<%for category in @categories%\>  
\<%for sub in category.sub\_categories%\>  
\<%=sub.name%\>  
\<%end%\>  
\<%end%\>

Check out  
[http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html](http://rails.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html)  
for more info on associations.

Matthew M.  
[blog.mattmargolis.net](http://blog.mattmargolis.net)

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [June 17, 2006, 9:32am UTC](https://www.ruby-forum.com/t/having-trouble-listing-tiers-of-categories/63668/3 "2006-06-17T09:32:54Z")

</div>

On Jun 16, 2006, at 10:01 PM, Josh P. wrote:

> 3 Siamese 5
> 
> Cat  
> –Siamese  
> –Persian
> 
> Right now it’s only listing them in one long list. But I’d like to  
> list  
> each parent category and have the subcats listed under them.

Recursion is the best way I know of to handle these things.

Here’s something that shows a tree correctly in text, I’ll  
leave it to you to convert the output to HTML, I think I’d  
choose nested lists.

This code is called via:  
ShowOrganizations.as\_tree

class ShowOrganizations  
def self.as\_tree  
show Organization.find\_by\_name(‘Root’),0  
end

```
 def self.show(organization,level)
   indent = ' ' * level
   puts indent + 'org:' + organization.name
   organization.children.each { |o| show o,level + 1 }
 end

```

end

Additionally, if you want something pre-manufactured with  
AJAX goodness, check out the LiveTree system.

[http://www.epiphyte.ca/code/live\_tree.html](http://www.epiphyte.ca/code/live_tree.html)

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [June 17, 2006, 9:30pm UTC](https://www.ruby-forum.com/t/having-trouble-listing-tiers-of-categories/63668/4 "2006-06-17T21:30:56Z")

</div>

Matthew M. wrote:

> Josh P. wrote:
> 
> > 4 Shih Tzu 1  
> > Cat  
> > –Siamese  
> > –Persian
> > 
> > Right now it’s only listing them in one long list. But I’d like to list  
> > each parent category and have the subcats listed under them.
> > 
> > Any ideas?
> 
> If you can I would recommend redoing your db and models so that you have  
> categories and sub\_categories. That would make this really easy. All  
> you would need to do is setup a category has\_many :sub\_categories and  
> rails would basically do the rest for you.  
> For instance if you setup the association then you could put this in a  
> view (set @categories = Category.find(:all) in your controller)  
> \<%for category in @categories%\>  
> \<%for sub in category.sub\_categories%\>  
> \<%=sub.name%\>  
> \<%end%\>  
> \<%end%\>

Nice. That worked…sort of. Do new controller/helpers/views need to be  
created? I can’t figure out how I might could keep using the same ones.

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [August 27, 2007, 9:25am UTC](https://www.ruby-forum.com/t/having-trouble-listing-tiers-of-categories/63668/5 "2007-08-27T09:25:26Z")

</div>

Josh P. wrote:

> I have categories and sub-categories in a table called “categories”.  
> Every row has a “parent” field to note if the entry is the sub-category  
> of another (never goes deeper than 1 level).
> 
> So there might be something like this:
> 
> id | name | parent  
> 1 Dog 0  
> 2 Pug 1  
> 3 Siamese 5  
> 4 Shih Tzu 1  
> 5 Cat 0  
> 6 Wiener Dog 1  
> 7 Persian 5
> 
> I’d like to list it like  
> Dog  
> –Pug  
> –Shih Tzu  
> –Wiener Dog
> 
> Cat  
> –Siamese  
> –Persian
> 
> Right now it’s only listing them in one long list. But I’d like to list  
> each parent category and have the subcats listed under them.
> 
> Any ideas?

I am having exact same problem – I have been searching, asking, begging  
for weeks…but have yet to see a good way to handle the selection of a  
category.

[http://tinyurl.com/28krhc](http://tinyurl.com/28krhc) #=\> how do I do this with rails? (eBay  
category selection).

I’m building a store and when I edit a product, i want to easily select  
its category…which could be a sub-category, etc.etc.

---

<div class="post-metadata">

**Author:** ![jpigford](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5a626/32.png) [@jpigford](https://www.ruby-forum.com/u/jpigford)\
**Post date:** [June 19, 2006, 8:13am UTC](https://www.ruby-forum.com/t/having-trouble-listing-tiers-of-categories/63668/6 "2006-06-19T08:13:45Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Sat, Jun 17, 2006 at 07:01:16AM +0200, Josh P. wrote:

> I have categories and sub-categories in a table called “categories”.  
> Every row has a “parent” field to note if the entry is the sub-category  
> of another (never goes deeper than 1 level).
> 
> So there might be something like this:
> 
> id | name | parent

Rename the ‘parent’ column to ‘parent\_id’. Your model:

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

See the following URL for usage info, it’ll do all you want:

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

You shouldn’t use the “sub-categories” solution suggested because you’d  
have two nearly-identical models you’d have to keep in sync and other  
such pain. Keep your code nice and general.

* * *

Peter H. - [http://push.cx](http://push.cx)

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2.2 (GNU/Linux)  
Comment: If you don’t know what this is, it’s OK to ignore it.

iD8DBQFElj/sa6PWv6+ALKoRAu/UAJ9AG3Oio+jXvakpJZbrsf6VT5QGGwCeNtCk  
gBx8V0Xz20Px3zyBOehWfGs=  
=nZGf  
-----END PGP SIGNATURE-----
