# Generating a Grouped Output

**URL:** <https://www.ruby-forum.com/t/generating-a-grouped-output/116693>\
**Category:** Rails\
**Created:** [October 8, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/generating-a-grouped-output/116693 "2007-10-08T19:49:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Austin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@Martin\_Austin](https://www.ruby-forum.com/u/Martin_Austin)\
**Post date:** [October 8, 2007, 7:49pm UTC](https://www.ruby-forum.com/t/generating-a-grouped-output/116693/1 "2007-10-08T19:49:08Z")

</div>

Hi all, first time poster.

I am currently learning RoR, and have run into a snag. I have a table  
called Games that has several fields, including title, desc, genre,  
system, etc. What I am looking to do in a view is create an output  
that is grouped by system. For example…

Wii  
… Wii Sports  
… Metroid Prime 3  
Xbox 360  
… Halo 3

My first thought was to use nested for loops, looping through each  
unique system, and then looping through each game that matches that  
system, and while I know how to do this in PHP (using procedural  
code), I am struggling with how to contain my iteration objects within  
the controller, and access them only in the view.

Any thoughts? I hope I am explaining this well.

Thanks,

Martin

---

<div class="post-metadata">

**Author:** ![Martin\_Austin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@Martin\_Austin](https://www.ruby-forum.com/u/Martin_Austin)\
**Post date:** [October 9, 2007, 4:41am UTC](https://www.ruby-forum.com/t/generating-a-grouped-output/116693/2 "2007-10-09T04:41:18Z")

</div>

Welcome, Martin.

The Rails API docs [http://api.rubyonrails.org](http://api.rubyonrails.org) show that  
ActiveRecord::Base#find[http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001004](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001004)can  
take a :group option to do what you want. For example,

Game.find(:all, :group =\> “system”)

Craig

---

<div class="post-metadata">

**Author:** ![Martin\_Austin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@Martin\_Austin](https://www.ruby-forum.com/u/Martin_Austin)\
**Post date:** [October 9, 2007, 4:41am UTC](https://www.ruby-forum.com/t/generating-a-grouped-output/116693/3 "2007-10-09T04:41:53Z")

</div>

Sorry for the font size weirdness in my reply (or is it only showing for  
me?).

---

<div class="post-metadata">

**Author:** ![Martin\_Austin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@Martin\_Austin](https://www.ruby-forum.com/u/Martin_Austin)\
**Post date:** [October 9, 2007, 5:26am UTC](https://www.ruby-forum.com/t/generating-a-grouped-output/116693/4 "2007-10-09T05:26:24Z")

</div>

Em Seg 08 Out 2007, Craig D. escreveu:

> Sorry for the font size weirdness in my reply (or is it only showing for  
> me?).

Relax… 🙂  
We’ll forget about your top-post and HTML-post… 🙂

## – Davi V.

E-mail: removed\_email\_address@domain.invalid  
MSN : removed\_email\_address@domain.invalid  
GTalk : removed\_email\_address@domain.invalid  
Skype : davi vidal  
ICQ : 138815296

---

<div class="post-metadata">

**Author:** ![Martin\_Austin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@Martin\_Austin](https://www.ruby-forum.com/u/Martin_Austin)\
**Post date:** [October 9, 2007, 7:55pm UTC](https://www.ruby-forum.com/t/generating-a-grouped-output/116693/5 "2007-10-09T19:55:53Z")

</div>

I think you are not looking for the SQL group by clause, but rather  
grouping on the html view. You can do your find to include the  
necessary joins from games to system (maybe vice versa?) and then  
render a partial that contains the system and a nested partial to list  
games.

Not sure if this is the most efficient way, but it works and you get  
reusable partials.

-Jamal
