Hi!
I have a problem with belongs_to and has_many, the thing is
i have a category table which points to herself if it's a subcategory
of another category.
I just started using ruby on rails, and don't know how to implement
that or if it can be done (I assume it can).
Hi!
I have a problem with belongs_to and has_many, the thing is
i have a category table which points to herself if it's a subcategory
of another category.
I just started using ruby on rails, and don't know how to implement
that or if it can be done (I assume it can).
Look at acts_as_tree. It's a plugin in Rails 2.0, builtin in Rails 1.2.
Example:
class Task < ActiveRecord::Base
has_many :comments, :dependent=>:destroy
end
class Comment < ActiveRecord::Base
belongs_to :task
acts_as_tree :order=>'created_at'
end
Tasks have a hierarchy of comments with replies and replies can have replies
arbitrarily deep.
task = Task.find(id)
task.comments # top level comments
task.comments[0].comments # replies to first top level comment
comment is like a category, reply is like a sub-category.