Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to implement Categories and Subcategories with flexible depth in Rails?

I have a Category and Subcategory models in my project. I would like to have many sub-levels in a flexible way. I thought making a self referential "parent" foreign key but I'm not quite sure how to do it. Any ideas? Thanks!

Cat1
    Sub1
        SubSub1
        SubSub2
    Sub2
Cat2
    Sub1
Cat3
    Sub1
    Sub2
        SubSub1
like image 692
Ben Orozco Avatar asked Apr 20 '10 15:04

Ben Orozco


2 Answers

Try the acts_as_tree plug-in

like image 67
Tony Fontenot Avatar answered Nov 15 '22 00:11

Tony Fontenot


Awesome_nested_set is the best solution, imo, for these types of structures in rails. http://github.com/collectiveidea/awesome_nested_set

like image 27
Peer Allan Avatar answered Nov 15 '22 00:11

Peer Allan