DDDD
DDDD

Reputation: 3940

Where query with uniqueness

I want to query for Answers that are correct = true and only grab one answer if they have the same question id. I was trying uniq or distinct.

@answers = Answer.where(id: params[:answer_ids])
@correct_answers = @answers.where(correct: true, question_id: distinct).count

How would you only grab one answer of any answers with the same question_id?

Upvotes: 0

Views: 36

Answers (1)

Surya
Surya

Reputation: 16002

Try this:

@answers = Answer.joins(:question).where(id: params[:answer_ids])
@correct_answers = @answers.where(correct: true).distinct(:question_id)

Upvotes: 2

Related Questions