NomadicRiley
NomadicRiley

Reputation: 111

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.

Upvotes: 6

Views: 13750

Answers (2)

Carson Reinke
Carson Reinke

Reputation: 723

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.

Upvotes: 1

Shadwell
Shadwell

Reputation: 34774

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

MyClass.count(:all, :group => 'column1, column2')

Upvotes: 8

Related Questions