# Confused by group\_by

**URL:** <https://www.ruby-forum.com/t/confused-by-group-by/178066>\
**Category:** Rails\
**Created:** [October 28, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/confused-by-group-by/178066 "2009-10-28T18:31:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 28, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/confused-by-group-by/178066/1 "2009-10-28T18:31:06Z")

</div>

I just started using rails and am trying to learn fast but I am  
confused with what I have read about using group\_by and could use some  
help please.

I have two tables - students and evaluations  
students has many evaluations and evaluations have one student

The student record has id, name, and grad\_year columns  
The evaluations record has id, student\_id, eval\_date and many boolean  
columns.

I would like to get a report of the sum of each of the boolean columns  
for evaluations by grad\_year  
I would also like to include only items that have a sum \> 0 (i.t. if  
item1 is always false, then sum is zero and it should not be included  
in the report).

I have tried a bunch of things but cannot seem to figure it out.

My last attempt was close, but I am not sure if it is best nor what to  
put in the view to display the actual items.

My controller had something like the following (abbreviated to save  
space):

@evaluations = Student.all(:joins =\> :evaluations, :select  
“students.\*, sum(evaluations.item1) as sum\_eval1, sum  
(evaluations.item2) as sum\_item2”, :group =\> “students.grad\_year”)

I can get this query to work in the console, but am not sure how to  
utilize the results in the view.

Thanks, any help is appreciated.
