Hi all... I created a table having a tree structure using acts_as_tree. Now I need to access all the parents, grandparents etc upto the root of a particular node. Also all the children upto leaf node. I used particular_node_id.children() method to access the immediate children and particular_node_id.ancestors() to access the immediate parents of a node. Please help me to find all the parents upto root and all the children upto leaf of a particular node. Thanks Regards Suneeta -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I used this code to get all the ancestors for a given node: def ancestors(list=[]) if parent.nil? list << parent else list << parent parent.ancestors(list) end list.compact end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 29, 7:22 am, Suneeta Km <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi all... > I created a table having a tree structure using acts_as_tree. Now I > need to access all the parents, grandparents etc upto the root of a > particular node. Also > all the children upto leaf node. > I used particular_node_id.children() method to access the immediate > children > and particular_node_id.ancestors() to access the immediate parents of a > node. > > Please help me to find all the parents upto root and all the children > upto leaf of a particular node.You should use act_as_nested_set or better_nested_set. They are better optimized for this, and it has built-in methods to accomplish what you are asking for. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---