Grouping records on a show page
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.58I'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.58Jane Doe
4.99, 13.99, 21.22, 36.21开发者_JS百科, 55.66What am I missing?
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
精彩评论