# Getting all CHildren Acs\_as\_tree

**URL:** <https://www.ruby-forum.com/t/getting-all-children-acs-as-tree/113159>\
**Category:** Rails\
**Created:** [August 28, 2007, 9:03am UTC](https://www.ruby-forum.com/t/getting-all-children-acs-as-tree/113159 "2007-08-28T09:03:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [August 28, 2007, 9:03am UTC](https://www.ruby-forum.com/t/getting-all-children-acs-as-tree/113159/1 "2007-08-28T09:03:20Z")

</div>

Hello, i want t know where can i foundd how to get all the childs from a  
user id, including all, recursivily.

Regards,

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [August 28, 2007, 12:44pm UTC](https://www.ruby-forum.com/t/getting-all-children-acs-as-tree/113159/2 "2007-08-28T12:44:38Z")

</div>

# recursive methods to collet a tree; very slow, and source-eating

# given you have a Page acts\_as\_tree model.

# i’d check out acts\_as\_nested\_set if queries / speed is an issue, but

this works, # either way.

# call get\_tree to , um, get the tree

```
 def get_children(page, collection)
            page.children.each { |p|
            collection << p ; get_children(p, collection)
            }
    end

    def get_tree
            tree = []
            page = Page.find(params[:id])
            get_children(page, tree)
            render_text tree.collect{|p| p.id }.join(", ")
                    rescue
                    render_text "enter an id"
    end

```

then call get\_tree/10 and it will find the branch under page#10.  
hope this helps somewhat, but i would strongly reccomend looking up  
other things aside from this recursive func, as it is a very slow  
method, and if you have a lot of children, this could take ages.

either way, enjoy.  
shai

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [August 28, 2007, 2:20pm UTC](https://www.ruby-forum.com/t/getting-all-children-acs-as-tree/113159/3 "2007-08-28T14:20:02Z")

</div>

> ```
> def get_children(page, collection)
> page.children.each { |p|
> collection << p ; get_children(p, collection)
> }
> end
> 
> def get_tree
> tree = []
> page = Page.find(params[:id])
> get_children(page, tree)
> render_text tree.collect{|p| p.id }.join(", ")
> rescue
> render_text "enter an id"
> end
> 
> ```

how i have to call the method, because it throw a nil object, calling  
this way:  
\<removed\_email\_address@domain.invalid do |m|%\>  
\<%m.hijos.each do |c|%\>  
\<%=c%\>  
\<%end%\>  
\<%end%\>

because i have acts\_as\_tree in other model this is why  
@member.matix…, so in member i got has\_many :matrix

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [August 28, 2007, 2:49pm UTC](https://www.ruby-forum.com/t/getting-all-children-acs-as-tree/113159/4 "2007-08-28T14:49:09Z")

</div>

I found this, i think it will be helpful, the problem is if i specify,  
for example 4, to see the members in the level 4, it will bring all the  
members to level 4, but i just want the ones who ares in level 4, from  
determinate user  
def matrix(depth, list=[])  
unless depth.zero?  
children.each do |child|  
list.push(child.member.name)  
child.hijoss(depth-1, list)  
end  
end  
list  
end

i have tried:

def matrix2(depth)  
list = matrix(depth)-matrix(depth-1)  
end  
but i dont like that way, i think there could be a better solution like  
.include?, exclude, i dont know relly :P, please some help

regrads,

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [August 29, 2007, 7:26am UTC](https://www.ruby-forum.com/t/getting-all-children-acs-as-tree/113159/5 "2007-08-29T07:26:56Z")

</div>

PLeased some help:

i want to load an hash array like this, but i dont get it:

def All(depth, list=[])  
level=0  
unless depth.zero?  
level += 1  
list[level] = []  
children.each do |child|  
list[level].push(child.member)  
child.All(depth-1, list)  
end  
end  
list  
end

any ideas?

---

<div class="post-metadata">

**Author:** ![eagoweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/9de0a6/32.png) [@eagoweb](https://www.ruby-forum.com/u/eagoweb)\
**Post date:** [August 28, 2007, 2:59pm UTC](https://www.ruby-forum.com/t/getting-all-children-acs-as-tree/113159/6 "2007-08-28T14:59:08Z")

</div>

seems like i found with this :S  
def matrix(depth)  
list = matrixx(depth-1)  
list.flatten - matrixx(depth-2).flatten  
end
