Ashwin Yaprala
Ashwin Yaprala

Reputation: 2777

Activerecord group by count and user_id

I have two models with has_many associations.

User :has_many foos
Foo :belongs_to user

Foo.joins(:user)

I want to have a group by on Foo, where it returns with count and user_id. Means I want to know how many 'foos' with 'user_id'.

Eg:

user_id foos_count 
-------------
1     31
2     52

Upvotes: 3

Views: 773

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118271

Here is the query to meet the need :

Foo.group(:user_id).count

Upvotes: 4

Related Questions