xps15z
xps15z

Reputation: 1829

List in the view the total count of Users

I need help figuring out how to do the total count of users from the Questions table (based on sender_id) with what their gender is (from User's table gender attribute).

The example for output in the view would be:

345 males have asked a Question
221 females have asked a Question

I am working from the Questions model, but need to make the connection to the Users model to determine if the user is a male or female based on their sender_id from the Questions table and gender from the Users table.

Upvotes: 0

Views: 30

Answers (1)

John
John

Reputation: 2828

count_of_males = Question.joins(:user).where(users: {gender: 'male'}).uniq.count

Which you can then modify for the female count, as well.

This assumes that you have a belongs_to :user in your Question class, and that you only want to count each user once, even though they may have asked many questions.

Upvotes: 3

Related Questions