# Group\_by sum

**URL:** <https://www.ruby-forum.com/t/group-by-sum/229728>\
**Category:** Rails\
**Created:** [February 11, 2013, 11:12am UTC](https://www.ruby-forum.com/t/group-by-sum/229728 "2013-02-11T11:12:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Werner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@Werner](https://www.ruby-forum.com/u/Werner)\
**Post date:** [February 11, 2013, 11:12am UTC](https://www.ruby-forum.com/t/group-by-sum/229728/1 "2013-02-11T11:12:32Z")

</div>

Again I need some help with grouping&sum

Attributes:

user\_id, week\_id, project\_id, hours  
1 61 1 20  
1 62 1 5  
1 62 2 0  
1 61 2 15  
1 63

What I need is a table row per user  
61 62 63…  
35 5

View:  
\<% @hour\_users.group\_by(&:user\_id).each do |user, hours| %\>

  
\<% hours.each do |h|%\>  
\<td"\>\<%= h.hours %\>  
\<% end %\>  
  
\<% end %\>

This gives me a row with all week\_ids and

---

<div class="post-metadata">

**Author:** ![Werner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@Werner](https://www.ruby-forum.com/u/Werner)\
**Post date:** [February 11, 2013, 11:18am UTC](https://www.ruby-forum.com/t/group-by-sum/229728/2 "2013-02-11T11:18:23Z")

</div>

On 11 February 2013 10:11, Werner [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> <% end %>
> </tr>
> 
> ```
> 
> \<% end %\>
> 
> This gives me a row with all week\_ids and

You need to tell us the classes and relationships, but if you want a  
row per user why are you not starting  
@users.each do |user|

Colin

---

<div class="post-metadata">

**Author:** ![Werner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/f4b2a3/32.png) [@Werner](https://www.ruby-forum.com/u/Werner)\
**Post date:** [February 11, 2013, 11:22am UTC](https://www.ruby-forum.com/t/group-by-sum/229728/3 "2013-02-11T11:22:29Z")

</div>

Sorry the posting was send by mistake…  
this is the correct question:

Again I need some help with grouping&sum

Attributes:

user\_id, week\_id, project\_id, hours  
1 61 1 20  
1 62 1 5  
1 62 2 0  
1 61 2 15  
1 63 1 0

What I need is a table row per user like that:  
61 62 63(week\_id)  
35 5 0… Sum of hours per user of all projects, per week\_id

View:  
\<% @hour\_users.group\_by(&:user\_id).each do |user, hours| %\>

  
\<% hours.each do |h|%\>  
\<td"\>\<%= h.hours %\>  
\<% end %\>  
  
\<% end %\>

This gives me a row with all week\_id’s and the hours per user but not  
summed up by project\_id.  
How do I have to group that ?  
Thanks…
