# How do you display nested elements in their hierarchy?

**URL:** <https://www.ruby-forum.com/t/how-do-you-display-nested-elements-in-their-hierarchy/99731>\
**Category:** Rails\
**Created:** [February 1, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/how-do-you-display-nested-elements-in-their-hierarchy/99731 "2006-02-01T16:21:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![snlsn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@snlsn](https://www.ruby-forum.com/u/snlsn)\
**Post date:** [February 1, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/how-do-you-display-nested-elements-in-their-hierarchy/99731/1 "2006-02-01T16:21:55Z")

</div>

Hello;

I’d like some pointers on how to display nested items in their  
hierarchy. I’m developing a project that uses habtm (as opposed to “acts  
as tree”). I can interact with the elements using “parent”, “child” etc.  
fine. I just can’t figure out how to make the elements _display_ in  
their relationships.

For example, if Ruby on Rails is a child of Server-Side and a sibling to  
PHP, and Server-side is a child of Coding and a sibling to Application,  
and Coding is a child of Main, how do I get the contents of the database  
to display in a browser something like:  
Main  
Coding  
Server-Side  
Ruby on Rails  
PHP  
Application

If I sort the database by parent\_id, I get stuff grouped by parent\_id,  
not organized in the hierarchy. In some other scripting environment I  
would probably write some recursive routine but don’t know how to  
approach it with Rails.

Any tips or pointers would be appreciated. Even what terms to use in  
google - I’m getting bird-related sites when I search for “nested”!

Thanks

---

<div class="post-metadata">

**Author:** ![snlsn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@snlsn](https://www.ruby-forum.com/u/snlsn)\
**Post date:** [February 1, 2006, 6:55pm UTC](https://www.ruby-forum.com/t/how-do-you-display-nested-elements-in-their-hierarchy/99731/2 "2006-02-01T18:55:07Z")

</div>

Steve-

```
Here is a helper method I use with acts_as_tree to display a set of

```

nested

's. If you have the parent and children methods working  
then this should work for you as well. Make sure to set up a counter  
cache for your model that you use this on, otherwise you will end up  
calling tons of COUNT sql queries as you recurse through the children.

module ApplicationHelper  
def find\_all\_children(parent)  
if parent.children.size \> 0  
ret = ‘

’  
parent.children.each { |child|  
if child.children.size \> 0  
ret += ‘ - ’  
ret += link\_to “#{child.role}: #{child.last\_name}, #  
{child.first\_name} : #{child.login}”,  
:controller =\> ‘review’, :action =\>  
‘boss\_view’, :id =\> child  
ret += find\_all\_children(child)  
ret += ‘
’  
else  
ret += ‘ - ’  
ret += link\_to “#{child.role}: #{child.last\_name}, #  
{child.first\_name} : #{child.login}”,  
:controller =\> ‘review’, :action =\>  
‘boss\_view’, :id =\> child  
ret += ‘
’  
end  
}  
ret += ‘
’  
end  
end  
end

This will build and return a nested list just like you want. Make  
whatever changes to the model names and attributes you need.

Cheers-  
-Ezra

On Feb 1, 2006, at 7:21 AM, Steve Nelson wrote:

> For example, if Ruby on Rails is a child of Server-Side and a  
> PHP  
> Thanks
> 
> –
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![snlsn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b3f665/32.png) [@snlsn](https://www.ruby-forum.com/u/snlsn)\
**Post date:** [February 1, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/how-do-you-display-nested-elements-in-their-hierarchy/99731/3 "2006-02-01T20:57:54Z")

</div>

Ezra  
Thank you very much for that. I appreciate your example of a recursive  
method in Rails.

I’ll see if I can get it to work. Thanks again!
