# Enumerating over a Tree by Depth

**URL:** <https://www.ruby-forum.com/t/enumerating-over-a-tree-by-depth/73231>\
**Category:** Rails\
**Created:** [February 25, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/enumerating-over-a-tree-by-depth/73231 "2006-02-25T17:05:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jose](https://www.ruby-forum.com/u/jose)\
**Post date:** [February 25, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/enumerating-over-a-tree-by-depth/73231/1 "2006-02-25T17:05:10Z")

</div>

I have a model using acts\_as\_tree which is working fine.

Being new to Ruby I’m don’t know what enumerator is best to use for  
traversing a tree by depth. I can write one, but before I do I wanted  
to know if there is there an enumerator for traversing trees available  
that the community recommends?

Thank you,  
Jose

---

<div class="post-metadata">

**Author:** ![jose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jose](https://www.ruby-forum.com/u/jose)\
**Post date:** [February 25, 2006, 6:23pm UTC](https://www.ruby-forum.com/t/enumerating-over-a-tree-by-depth/73231/2 "2006-02-25T18:23:26Z")

</div>

Jose Hales-Garcia wrote:

> I have a model using acts\_as\_tree which is working fine.
> 
> Being new to Ruby I’m don’t know what enumerator is best to use for  
> traversing a tree by depth. I can write one, but before I do I wanted  
> to know if there is there an enumerator for traversing trees available  
> that the community recommends?

I don’t know if it contains an enumerator, but acts\_as\_nested\_set is  
more efficient than acts\_as\_tree if you need to do this kind of  
iteration more often.

---

<div class="post-metadata">

**Author:** ![jose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jose](https://www.ruby-forum.com/u/jose)\
**Post date:** [February 25, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/enumerating-over-a-tree-by-depth/73231/3 "2006-02-25T21:46:27Z")

</div>

Andreas S. wrote:

> I don’t know if it contains an enumerator, but acts\_as\_nested\_set is  
> more efficient than acts\_as\_tree if you need to do this kind of  
> iteration more often.

It looks as if acts\_as\_nested\_set is exactly what I am looking for.  
Thank you for the suggestion.

Jose

---

<div class="post-metadata">

**Author:** ![jose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jose](https://www.ruby-forum.com/u/jose)\
**Post date:** [February 25, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/enumerating-over-a-tree-by-depth/73231/4 "2006-02-25T23:14:41Z")

</div>

On 2/25/06, Jose Hales-Garcia [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Andreas S. wrote:
> 
> > I don’t know if it contains an enumerator, but acts\_as\_nested\_set is  
> > more efficient than acts\_as\_tree if you need to do this kind of  
> > iteration more often.
> 
> It looks as if acts\_as\_nested\_set is exactly what I am looking for.  
> Thank you for the suggestion.

You can also try the acts\_as\_threaded plugin:

[http://www.railtie.net/articles/2006/02/05/rails-acts\_as\_threaded-plugin](http://www.railtie.net/articles/2006/02/05/rails-acts_as_threaded-plugin)

–  
Rick O.  
[http://techno-weenie.net](http://techno-weenie.net)
