2

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

1 Answer 1

4
User.group(:type).count
=> {"A"=>4, "B"=>1, "C"=>10}
Sign up to request clarification or add additional context in comments.

2 Comments

I mean, is it possible to do something like this: User.group(:type).count.each do |n,m| puts(n,m)?
@Katja Yes you can iterate like this: User.group(:type).count.each do |type, count|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.