Hello,
I’m trying to create categories that have their own subcategories, and
each
subcategory may have its own subcategories, … and so on.
After some reading, I found out that the best way for creating such a
thing
is to use acts_as_tree, but I didn’t find any tutorial or article that
explain this in a clear way. After a lot of work I was finally able to
create categories and subcategories using the following code explained
in
(Agile Web D. With Rails) book:
create table categories (
id int not null auto_increment,
name varchar(100) not null,
parent_id int,
constraint fk_category foreign key (parent_id) references
categories(id),
primary key (id)
);
in the model:
class Category < ActiveRecord::Base
acts_as_tree :order => “name”
end
OK, my problem now is how to view something! Any category name, or any
subcategory or a list of categories, I tried a lot of things, like (in
the
view):
For my app, I needed to modify acts_as_tree (ActiveRecord::Acts::Tree)
such that deleting a node would orphan sub-nodes, rather than deleting
them.
If you have orphaned nodes, how do you know which one is root?
The whole point is that I want to be able to represent multiple trees,
not just one. There already is a method “roots” that works just fine
when there are such orphaned nodes. The method “root” ceases really
to have meaning, but that’s fine.