views:

18

answers:

1

In my Rails app, I have a Runs model, a Timesheets model, and an Athletes model. A Run belongs_to an Athlete, and an Athlete has_many Runs. A Run also belongs_to a Timesheet, and a Timesheet has_many Runs.

Timesheet/show/:id lists the runs associated with a particular timesheet, along with the athlete associated with each respective run.

On this page, it's common to have something like this:

Joe Smith 5.98, 14.98, 21.22, 36.33, 55.67
Jane Doe 4.99, 13.99, 21.22, 36.21, 55.66
Joe Smith 6.00, 14.87, 21.22, 36.31, 55.58

I'm sure this is a beginners mistake, but I don't know how to group the runs by each athlete, such as this:

Joe Smith
5.98, 14.98, 21.22, 36.33, 55.67
6.00, 14.87, 21.22, 36.31, 55.58

Jane Doe
4.99, 13.99, 21.22, 36.21, 55.66

What am I missing?

+1  A: 

The awesome group_by method on any enumerable!

so if you have @timesheets:

@timesheets.group_by(&runner_id).each do |runner_id, runs|
   runs.each{|run| puts run}
end
Jesse Wolgamott
Awesomeness! Knew I was missing something simple here. Thanks for your response.
jktress