Rails isl grouping and counting with associations

Given that I have a Member table:

id   membername
-------------------
1    Fred
2    Dave
3    Jenny

and jobs table

id   member_id   status
1    1           Closed
2    2           Pending
3    2           Open
4    3           Pending
5    3           Pending

I want to display the total number of pending jobs by a member.

So, with the above data, I would like to:

membername  count
Dave        1
Jenny       2

I can capture scores using a group method like

Job.where("status = ?", "Pending").group(:member_id).count

but it does not return names. Just a hash of member_id and counts. I think I need to somehow join this result in the participants table. Of course, I can do this with raw SQL, but I was wondering if there was a way to accurately capture this data.

+3
source share
1 answer
Job.where("status = ?", "Pending").joins(:member).group("member.name").count
+5
source

All Articles