Forum: Ruby on Rails How to build a recursive parenting structure?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bd3a9ca8a116e0055e65dd2d6069c946?d=identicon&s=25 Todd S. (tdog)
on 2006-02-27 04:56
I'm just getting used to using HABTM structures.  Something I'm trying
to do now is create a site that has "sections" each section then could
have a sub "section".

So I built a "sections" table and in the model I included the line
"has_and_belongs_to_many :sections"
I also made a table called "sections_sections" to build the heirarchy
however the fields I generated were:

CREATE TABLE  `sections_sections` (
 `section_id` INT( 10 ) UNSIGNED NOT NULL,
 `parent_id` INT( 10 ) UNSIGNED NOT NULL ,
 constraint fk_parents foreign key (parent_id) references sections(id),
 constraint fk_sections foreign key (section_id) references sections(id)
);

Because I can't have two section_id fields.  So how can I let rails know
that it should look for parent_id when I do something like:
@section.sections.include?(@section_to_look_for)?
Fac632373c881872ecf26d13aadedf7f?d=identicon&s=25 Joshua Bates (Guest)
on 2006-02-27 22:40
(Received via mailing list)
Sounds like what you're looking for is :acts_as_tree
Unless a section can have more than one parent, there is
no need for a HABTM table, just put the parent_id column
in the sections table, and add :acts_as_tree.

joshua
This topic is locked and can not be replied to.