6

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
jdl
  • 17,702
  • 4
  • 51
  • 54
Ben Orozco
  • 4,361
  • 4
  • 33
  • 49

2 Answers2

5

Try the acts_as_tree plug-in

Tony Fontenot
  • 5,091
  • 1
  • 21
  • 19
5

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

Peer Allan
  • 3,934
  • 2
  • 21
  • 17
  • Agreed. It's nasty when it comes to writes, but when it comes to reads, it kicks ass. You can get the whole tree structure with one query. – changelog Apr 21 '10 at 12:40