Generating a Grouped Output

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 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.



Welcome, Martin.

The Rails API docs show that
can take a :group option to do what you want. For example,

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


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

Relax... :slight_smile:
  We'll forget about your top-post and HTML-post... :slight_smile:

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

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