Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This tree utility methods assume that the models have appropriate relationships already defined.
- # For example, `Node` class should have `children` associations pointing to children nodes.
- def traverse(root_node)
- puts root_node.name
- root_node.children.each {|c| traverse(c)} unless root_node.children.empty?
- end
- def copy_tree(root_node)
- root_copy = root_node.dup
- root_copy.save! #if rails, set next available id instead
- root_node.children.each {|c| root_copy.children << copy_tree(c)} unless root_node.children.empty?
- root_copy
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement