get_nodes(root_id, nodes, level) first_node = Node.find(root_id) level += 1 if first_node.nil? or first_node.children.nil? return end first_node.children.each do |node| nodes << {:node_id =>node.id,:level =>level} get_nodes(node.id, nodes, level) end nodes end