# Grouping a list of links by category

**URL:** <https://www.ruby-forum.com/t/grouping-a-list-of-links-by-category/57809>\
**Category:** Rails\
**Created:** [April 14, 2006, 1:30pm UTC](https://www.ruby-forum.com/t/grouping-a-list-of-links-by-category/57809 "2006-04-14T13:30:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrtnhndrkx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mrtnhndrkx](https://www.ruby-forum.com/u/mrtnhndrkx)\
**Post date:** [April 14, 2006, 1:30pm UTC](https://www.ruby-forum.com/t/grouping-a-list-of-links-by-category/57809/1 "2006-04-14T13:30:51Z")

</div>

Hi,

I want to display a list of links grouped by category like this:  
Group 1  
Link 1  
Link 2  
Group 2  
Link 1  
Link 2

I have this in my controller:  
def index  
@links = Link.find(:all, :order =\> “category\_id”)  
end

And this in the view:

 \<% for link in @links %\> - \<%= link.link %\>
 \<% end %\> 

Can somebody give my some pointers how to achieve this?  
Thanks!

Maarten

---

<div class="post-metadata">

**Author:** ![mrtnhndrkx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mrtnhndrkx](https://www.ruby-forum.com/u/mrtnhndrkx)\
**Post date:** [April 14, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/grouping-a-list-of-links-by-category/57809/2 "2006-04-14T16:34:42Z")

</div>

Maarten,

On Fri, Apr 14, 2006 at 01:30:51PM +0200, Maarten H. wrote:

> I want to display a list of links grouped by category like this:  
> Group 1  
> Link 1  
> Link 2  
> Group 2  
> Link 1  
> Link 2

What a coinsidence. I just set up a similar link section yesterday.

> Can somebody give my some pointers how to achieve this? Thanks!

I’m very new to Rails so there it is very likely that there is a better  
way  
to do this. But, the way I did it was:

In my Link controller:

```
def list
    @categories = LinkCategory.find(:all, :order => 'name')
end

```

And in my list.rhtml view something along the lines of:

\<% for category in @categories -%\>

```
<h2><%= h(category.name) %></h2>

<% for link in category.links -%>

    <p><%= link_to h(link.name), link.url %></p>

<% end -%>

```

\<% end -%\>

Kevin  
[http://www.RawFedDogs.net](http://www.RawFedDogs.net)  
[http://www.WacoAgilityGroup.org](http://www.WacoAgilityGroup.org)  
Bruceville, TX
