Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group by multiple columns in ActiveRecord

Using Rails 3.0, i'm trying to do a count on number of times a each combination of column1 and column2 occur.

IE Column A has values A-Z and Column B has values 1-5, i want a count of A1, A2, etc.

Is there a way to either group by multiple columns or join the two columns and group of the result? In reading the documentation, it wasn't clear how to accomplish this.

like image 856
NomadicRiley Avatar asked Sep 23 '10 16:09

NomadicRiley


2 Answers

You should be able to specify multiple attributes to group by. Something like:

MyClass.count(:all, :group => 'column1, column2')
like image 174
Shadwell Avatar answered Sep 30 '22 06:09

Shadwell


I believe this is what you want to do: rails group by multiple columns. The calculations cannot handle the multiple column group by. You can also use find_by_sql.

like image 34
Carson Reinke Avatar answered Sep 30 '22 06:09

Carson Reinke