I have my model 'Person' with Ancestry and I am creating my family tree. In this case, I'm trying to get the first 3 levels of a node, for example:
Starting from the father (1st level), I just want to get all their children (2nd level) and all their grandchildren (3rd level). But I do not want to get their great-grandson (4th level).
Now, if we start with one of the sons (1st level), I want to get your kids (2nd level) and grandchildren of that son (3rd level).
This is the code that I have, but with this obtain all level for a node:
def self.get_tree_json(user)
json_hash = Hash.new
if user.has_children?
array_children = Array.new
user.children.each do |child| # This function get all children
array_children << get_tree_json(child)
end
json_hash["children"] = array_children
end
return json_hash
end