class Node < ActiveRecord::Base attr_accessible :name belongs_to :parent_node, :class_name => "Node", :foreign_key => :parent_id has_many :child_nodes, :class_name => "Node", :foreign_key => :parent_id end
For the root node, parent_id will be nill.
Say I have the id of the root node, and wish to print the entire tree to the output stream in json format.
How do I go about doing that?
Assume the tree does not contain any loops.
Sounds like you need awesome_nested_set. The wiki is well documented but can be hard to find if you don’t know it’s there.
In your case, you’ll want to do something like: