I have a branch model that consists of parent_id and child_id. I want to get an array of parent/children relations without querying each parent for its children.
For this table:
Parent_id | Child_id
1 | 2
1 | 6
1 | 9
2 | 3
3 | 10
4 | 7
I want to get 1's children, and his childrens' children like this:
{2 => {3 => {10}}, 6, 9}
without querying each parent for its children.
Is there an algorithm to achieve this efficiently or do I need to go through each parent? Thanks.