# Category and subcategories in database

**URL:** <https://www.ruby-forum.com/t/category-and-subcategories-in-database/59827>\
**Category:** Rails\
**Created:** [May 7, 2006, 5:56am UTC](https://www.ruby-forum.com/t/category-and-subcategories-in-database/59827 "2006-05-07T05:56:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Mannakee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@Mike\_Mannakee](https://www.ruby-forum.com/u/Mike_Mannakee)\
**Post date:** [May 7, 2006, 5:56am UTC](https://www.ruby-forum.com/t/category-and-subcategories-in-database/59827/1 "2006-05-07T05:56:44Z")

</div>

I have a project I’m trying to decide if I want to use rails for. I’m  
new  
to rails, so I’m not sure how easy it would be to implement something  
like  
this using ActiveRecord. Here’s the situation:

Category \> subcategory \> subcategory \> product  
Category \> subcategory \> product

Some categories will have many subcategories, others few. I want to use  
adjacency lists in the database like

ID CATEGORY PARENT

where top-level categories simply have no parent, but subcategories do.  
My  
concern is whether Rails, using ActiveRecord, can be made to understand  
this. I’m coming from PHP and know I can code it there. The question  
is  
can I do this same thing in Rails, and what will it involve? Does  
anyone  
have any experience that will help?

Mike

---

<div class="post-metadata">

**Author:** ![Mike\_Mannakee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@Mike\_Mannakee](https://www.ruby-forum.com/u/Mike_Mannakee)\
**Post date:** [May 7, 2006, 6:02am UTC](https://www.ruby-forum.com/t/category-and-subcategories-in-database/59827/2 "2006-05-07T06:02:49Z")

</div>

ActiveRecord is great for this kind of model.

[http://api.rubyonrails.org/classes/ActiveRecord/Acts/Tree/](http://api.rubyonrails.org/classes/ActiveRecord/Acts/Tree/)  
ClassMethods.html

Brandon

---

<div class="post-metadata">

**Author:** ![Mike\_Mannakee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc8723/32.png) [@Mike\_Mannakee](https://www.ruby-forum.com/u/Mike_Mannakee)\
**Post date:** [May 7, 2006, 6:05am UTC](https://www.ruby-forum.com/t/category-and-subcategories-in-database/59827/3 "2006-05-07T06:05:54Z")

</div>

Hi Mike,

ActiveRecord can do all that you want. I have implemented exactly this  
system in the store.rb schema.

Project  
[http://trac.vaillant.ca/store.rb/](http://trac.vaillant.ca/store.rb/)

Schema  
[http://trac.vaillant.ca/store.rb/browser/branches/characters/db/tables.sql](http://trac.vaillant.ca/store.rb/browser/branches/characters/db/tables.sql)

demo store  
[http://characters.michaux.ca/](http://characters.michaux.ca/)

Hopefully these links can jumpstart you. That is part of the reason  
for the project.

Peter
