Group by with ActiveRecord in Rails

Posted by Adnan on Stack Overflow See other posts from Stack Overflow or by Adnan
Published on 2010-03-12T08:33:49Z Indexed on 2010/03/12 8:37 UTC
Read the original article Hit count: 179

Hello,

I have a the following table with rows:

================================================================
id | name | group1 | group2 | group3 | group4 | ================================================================
1 | Bob | 1 | 0 | 0 | 1|
================================================================
2 | Eric| 0 | 1 | 0 | 1|
================================================================
3 | Muris | 1 | 0 | 1 | 1|
================================================================
4 | Angela | 0 | 0 | 0 | 1|
================================================================

What would be the most efficient way to get the list with ActiveRecords ordered by groups and show their count like this:

group1 (2)
group2 (1)
group3 (1)
group4 (4)

All help is appreciated.

© Stack Overflow or respective owner

Related posts about activerecord

Related posts about ruby-on-rails