I am working on an Rails application that uses a pretty complex category structure through out the site. I have defined a table to house all the info and a FK to reference parents within the table CREATE TABLE categories ( id int(11) NOT NULL auto_increment, name varchar(50) NOT NULL, parent_id int(11) default NULL, constraint fk_category_id foreign key (category_id) references categories(id), PRIMARY KEY (id) ); My goal was to be able to set the names to reference other entries in the table, establishing the parent/child relationship parent = id: 1 name: Category 1 parent_id: NULL child = id: 2 name: child 1 parent_id: 1 child = id: 3 name: child 2 parent_id: 2 My Model starts out as: class Category < ActiveRecord::Base acts_as_tree :order => "name" belongs_to :catagory, :class_name => "Category", :foreign_key => "parent_id" has_many :sub_category, :class_name => "Category", :foreign_key => "parent_id" Which seems right I know that acts_as_nested_set has the ability to traverse through the structure but everything I hear says not to use it yet. My question is more or less can I till use it''s methods? Should I? Or would the best practice be to write a helper for this? I am a bit new to Rails but this is really stumping me, or maybe I''m looking a bit to hard into it. J Brien | HybridIndie Productions | hybridindie.com | iam-QLwuMy0vUAhCl22TKe+ceQ@public.gmane.org
Adrian Madrid
2005-Jul-26 17:00 UTC
Re: acts_as_tree and traversing parent/child relationships
The nested_set approach will require a different schema: you need left and right indicators. The good news is that you will not need the belongs to and all that, it''s just a tree. Hope it helps, Adrian Madrid On 7/25/05, J Brien <iam-QLwuMy0vUAhCl22TKe+ceQ@public.gmane.org> wrote:> > I am working on an Rails application that uses a pretty complex > category structure through out the site. I have defined a table to > house all the info and a FK to reference parents within the table > > CREATE TABLE categories ( > id int(11) NOT NULL auto_increment, > name varchar(50) NOT NULL, > parent_id int(11) default NULL, > constraint fk_category_id foreign key (category_id) references > categories(id), > PRIMARY KEY (id) > ); > > My goal was to be able to set the names to reference other entries in > the table, establishing the parent/child relationship > > parent = id: 1 name: Category 1 parent_id: NULL > child = id: 2 name: child 1 parent_id: 1 > child = id: 3 name: child 2 parent_id: 2 > > My Model starts out as: > > class Category < ActiveRecord::Base > acts_as_tree :order => "name" > > belongs_to :catagory, :class_name => "Category", :foreign_key => > "parent_id" > has_many :sub_category, :class_name => "Category", :foreign_key > => "parent_id" > > Which seems right > > I know that acts_as_nested_set has the ability to traverse through > the structure but everything I hear says not to use it yet. My > question is more or less can I till use it''s methods? Should I? Or > would the best practice be to write a helper for this? > > I am a bit new to Rails but this is really stumping me, or maybe I''m > looking a bit to hard into it. > > J Brien | HybridIndie Productions | hybridindie.com<http://hybridindie.com>| > iam-QLwuMy0vUAhCl22TKe+ceQ@public.gmane.org > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Adrian Esteban Madrid aemadrid-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Lou Vanek
2005-Aug-07 07:36 UTC
Re: acts_as_tree and traversing parent/child relationships
If you haven''t implemented a tree module yet you may want to consider sqltree.rb (http://rubyforge.org/frs/?group_id=835). Full disclosure: I wrote the d*mn thing. I use the module myself, but not with Rails (yet). [caveat: it works best with MySQL] -lv J Brien wrote:> I am working on an Rails application that uses a pretty complex > category structure through out the site. I have defined a table to > house all the info and a FK to reference parents within the table >[snip,snap,crackle,pop]> > I know that acts_as_nested_set has the ability to traverse through the > structure but everything I hear says not to use it yet. My question is > more or less can I till use it''s methods? Should I? Or would the best > practice be to write a helper for this? > > I am a bit new to Rails but this is really stumping me, or maybe I''m > looking a bit to hard into it. > > J Brien | HybridIndie Productions | hybridindie.com | > iam-QLwuMy0vUAhCl22TKe+ceQ@public.gmane.org