Alina
Alina

Reputation: 2261

Ruby on Rails, count records of certain types

I have 3 different types of records. Let say 4 records are of type A, 1 record is of type B and 10 of type C. Some users have only A and B, and others all 3 types.

First, I need to retrieve the types, and then count how many records have the type A, B or C.

Is it possible to make it in one call, so that I get a Hash from the database like following:

 A:4, B:1, C:10

Upvotes: 2

Views: 727

Answers (1)

ck3g
ck3g

Reputation: 5929

User.group(:type).count
=> {"A"=>4, "B"=>1, "C"=>10}

Upvotes: 4

Related Questions