# Creating a Hierarchical View

**URL:** <https://www.ruby-forum.com/t/creating-a-hierarchical-view/171323>\
**Category:** Rails\
**Created:** [June 30, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/creating-a-hierarchical-view/171323 "2009-06-30T18:52:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [June 30, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/creating-a-hierarchical-view/171323/1 "2009-06-30T18:52:18Z")

</div>

I plan on creating a table of topics. The thing is that the topics  
are hierarchical in nature. That is, there may be any number of root  
topics. Each root topic can have other topics which would be like sub-  
topics; and, sub-topics can have their own sub-topics, etc. I was  
thinking about establishing the relationship by having a ‘parent’  
column (sort of like ‘belongs\_to’) in the topics table so that the  
parent topic of any given topic would always be known. However, I’m  
not married to this idea. In any event, the problem comes in creating  
the show view. I’d like to display the topics using a series of  
embedded ULs so that the view not only shows the topics but also shows  
them in a way that displays their hierarchical relationships. I have  
no idea how I am going to do this. Any ideas?

Thanks for any input.

```
       ... doug
```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [June 30, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/creating-a-hierarchical-view/171323/2 "2009-06-30T18:52:19Z")

</div>

2009/6/30 doug [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> embedded ULs so that the view not only shows the topics but also shows  
> them in a way that displays their hierarchical relationships. Â I have  
> no idea how I am going to do this. Â Any ideas?

If I understand correctly you are looking for a tree structure for the  
topics. Have a look at the acts\_as\_tree plugin. Google will provide  
lots of links I think. There is a railscasts at

> **[\#162 Tree Based Navigation - RailsCasts](http://railscasts.com/episodes/162-tree-based-navigation)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Colin

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [June 30, 2009, 9:10pm UTC](https://www.ruby-forum.com/t/creating-a-hierarchical-view/171323/3 "2009-06-30T21:10:40Z")

</div>

Colin L. wrote:

> 2009/6/30 doug [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > embedded ULs so that the view not only shows the topics but also shows  
> > them in a way that displays their hierarchical relationships. Â I have  
> > no idea how I am going to do this. Â Any ideas?
> 
> If I understand correctly you are looking for a tree structure for the  
> topics. Have a look at the acts\_as\_tree plugin.  
> […]

I would generally recommend a nested-set model instead;  
awesome\_nested\_set works well. The nested set structure is less obvious  
than the simple adjacency list that acts\_as\_tree provides, but it makes  
certain common operations _much_ more efficient.

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [July 1, 2009, 6:29pm UTC](https://www.ruby-forum.com/t/creating-a-hierarchical-view/171323/4 "2009-07-01T18:29:00Z")

</div>

> I would generally recommend a nested-set model instead;

Thanks for the suggestion. I see the nested-set model as being an  
alternative to acts\_as\_tree. Unless I am mistaken they are 2  
different ways of doing essentially the same thing. Both could be  
helpful in creating a hierarchical view but neither provides a slam-  
dunk approach to that end.

I think I see where I stand. Thanks to all who responded.

```
    ... doug

```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [July 1, 2009, 7:05pm UTC](https://www.ruby-forum.com/t/creating-a-hierarchical-view/171323/5 "2009-07-01T19:05:06Z")

</div>

Doug J. wrote:

> > I would generally recommend a nested-set model instead;
> 
> Thanks for the suggestion. I see the nested-set model as being an  
> alternative to acts\_as\_tree. Unless I am mistaken they are 2  
> different ways of doing essentially the same thing.

Yes. Nested sets are unquestionably the superior alternative – for  
example, with nested sets, it is possible to retrieve a node and all its  
descendants from the DB with a _single_ query. This is impossible with  
the simple adjacency list that acts\_as\_tree provides.

> Both could be  
> helpful in creating a hierarchical view but neither provides a slam-  
> dunk approach to that end.

What do you mean by a slam-dunk appropach? Something that generates the  
HTML? So far as I know, you’ll have to do that yourself.

> I think I see where I stand.

And where is that?

> Thanks to all who responded.
> 
> ```
> ... doug
> 
> ```

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [July 1, 2009, 8:50pm UTC](https://www.ruby-forum.com/t/creating-a-hierarchical-view/171323/6 "2009-07-01T20:50:23Z")

</div>

> different ways of doing essentially the same thing. Both could be  
> helpful in creating a hierarchical view but neither provides a slam-  
> dunk approach to that end.

Rails still requires “programming,” after all. Are you having trouble  
creating an unordered list in your view or something?

-e

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [June 30, 2009, 9:36pm UTC](https://www.ruby-forum.com/t/creating-a-hierarchical-view/171323/7 "2009-06-30T21:36:26Z")

</div>

> If I understand correctly you are looking for a tree structure for the  
> topics. Have a look at the acts\_as\_tree plugin. Google will provide  
> lots of links I think. There is a railscasts athttp://railscasts.com/episodes/162-tree-based-navigation

Thanks for the input, Colin.

I watched the railscast that you suggested. It’s definitely dealing  
with the type of thing that I am talking about. However (and I may be  
totally naive on this) it seems that acts\_as\_tree really only does 2  
things: (1) it does the grunt work of setting up the associations;  
and, (2) it provides a few handy instance methods.

The point is that while having those capabilities available in a nice  
package is handy, there doesn’t seem to be anything in the package  
that will help me develop the code to generate the basic framework for  
the hierarchical view. That’s the part that I don’t have a clue as to  
how I am going to accomplish.

Again, thanks for the input.

```
      ... doug

```

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [July 2, 2009, 6:27pm UTC](https://www.ruby-forum.com/t/creating-a-hierarchical-view/171323/8 "2009-07-02T18:27:23Z")

</div>

You need to look at both client side representation and the server  
side data model. For server side, people have been pointing out  
solutions here, e.g., acts\_as\_tree, nested sets etc. Then there is  
the client side representation of this, which has not been mentioned.

Recently, I did a project (actually, I am still working on it) where  
both sides are important. I used acts\_as\_tree to begin with and have  
moved on to acts\_as\_adjacency\_list since it has a few improvements  
that I need.

For the client side, you have two options:

1. You can either represent the menu trail by a series of linked  
“breadcrumbs” which show exactly how far deep you are into the  
hierarchy and allow you to climb-back or drill-down further in the  
tree. This does not require Javascript and can be done by simple and  
traditional view coding.
2. For fancier navigation where you actually show the tree visually,  
you will need Javascript. I am using jQuery based jsTree though there  
are a number of other alternatives. You will need some Javascript/  
jQuery skills though to go down this path. Take a look at the Spree  
project that uses both quite effectively. Actually, I am learning a  
lot just digging down into the project code base and adapting it to my  
own needs - Highly recommended.

Hope this helps.

Bharat

---

<div class="post-metadata">

**Author:** ![djames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ab992/32.png) [@djames](https://www.ruby-forum.com/u/djames)\
**Post date:** [July 2, 2009, 2:53am UTC](https://www.ruby-forum.com/t/creating-a-hierarchical-view/171323/9 "2009-07-02T02:53:16Z")

</div>

> Rails still requires “programming,” after all. Are you having trouble  
> creating an unordered list in your view or something?

It probably is an ordered tree in a view…? One way I can think of  
would be to catch all root elements (usually the ones with a nil  
parent\_id!) together with der descendants into an array of arrays and  
iterate over this with @root\_elements\_and\_all\_its\_descendents.each do |  
x|{ x.each do |z|{z.xyz}} - is that the problem?
