ruby on rails - Combining group_by and sum -
i have model
next attributes:
user_id, week_id, project_id, hours 1 61 1 20 1 62 1 5 1 61 2 15 1 63 1 0 2 61 1 0
my view
this:
<tr> 61 | 62 | 63 (week_id) 35 | 5 | 0 (sum of hours per week_id) </tr> all_hours = model.group_by(&:user_id) @all_hours.each |user, hours| %> <tr> hours.each | hr | hour.my_hours
and gives me 2 lines of user 1
how grouping sec time & sum hours per week_id wanted result?
to create more clear (hopefully) :
@hours= model.all.group_by(&:user_id) <% @hours.each |user, hours| %> <tr> <% hours.each |h|%> <td><%= h.hours%></td> <% end %> </tr>
gives me 1 long row per user like: 0.0 20.0 0.0 .... 0.0 15.0 0.0 want sum 20 , 15 within line 0.0 35.0 0.0
i give seek rails 3:
model.select(:week_id). select(model.arel_table[:hours].sum.as("sum_of_hours")).select(:user_id) group(:week_id).group(:user_id)
the library used here arel
, part of rails 3, activerecord.
i assume works. haven't tried myself. hope helps.
ruby-on-rails ruby-on-rails-3 group-by sum
No comments:
Post a Comment