# How to display categorized pages in the most effective way?

**URL:** <https://www.ruby-forum.com/t/how-to-display-categorized-pages-in-the-most-effective-way/169885>\
**Category:** Rails\
**Created:** [June 9, 2009, 11:14pm UTC](https://www.ruby-forum.com/t/how-to-display-categorized-pages-in-the-most-effective-way/169885 "2009-06-09T23:14:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![emmek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a698b9/32.png) [@emmek](https://www.ruby-forum.com/u/emmek)\
**Post date:** [June 9, 2009, 11:14pm UTC](https://www.ruby-forum.com/t/how-to-display-categorized-pages-in-the-most-effective-way/169885/1 "2009-06-09T23:14:59Z")

</div>

I am looking for the most effective way to display categorized pages.

Page model:  
acts\_as\_tree  
belongs\_to :theme

(has attribute “title”)

Theme model:  
has\_many :pages

(has attribute “name”)

I’d like to display all the children (titles) of the current page but  
grouping these pages in themes they belong to.

I imagine that code “extracts” unique themes from @page.children and  
then iterates through these themes and displays just pages (titles) that  
are children of the current page and belong to proper themes.

This should print the following data:

Theme\_X

- page\_b
- page\_f
- page\_m  
Theme\_Y
- page\_c
- page\_g
- page\_l  
Theme\_ …

where page\_? is the child of the current page.

How to do it effectively? Any help appreciated.

---

<div class="post-metadata">

**Author:** ![emmek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a698b9/32.png) [@emmek](https://www.ruby-forum.com/u/emmek)\
**Post date:** [June 10, 2009, 2:30pm UTC](https://www.ruby-forum.com/t/how-to-display-categorized-pages-in-the-most-effective-way/169885/2 "2009-06-10T14:30:16Z")

</div>

Emmek on Rails wrote:

> I imagine that code “extracts” unique themes from @page.children and  
> then iterates through these themes and displays just pages (titles) that  
> are children of the current page and belong to proper themes.

You might want to look at named\_scopes… this sounds like a candidate  
for a something like page.theme\_ordered\_children, rather than just  
page.children.

---

<div class="post-metadata">

**Author:** ![emmek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a698b9/32.png) [@emmek](https://www.ruby-forum.com/u/emmek)\
**Post date:** [June 11, 2009, 6:05pm UTC](https://www.ruby-forum.com/t/how-to-display-categorized-pages-in-the-most-effective-way/169885/3 "2009-06-11T18:05:38Z")

</div>

OK, I was given the complete solution. I have the following pieces of  
code:

controller:

@thematized\_pages = @page.children.group\_by(&:theme)

view:

\<% @thematized\_pages.each do |theme, pages| -%\>

### \<%= theme ? theme.name : 'Others' %\>

 \<% pages.each do |page| %\> - \<%= link\_to h(page.title), h(page.parent.link\_name+'/'+page.link\_name) %\>
 \<% end -%\>
