哪里查询具有唯一性

时间:2014-11-12 18:37:23

标签: ruby-on-rails ruby-on-rails-4

我想查询正确=真的答案,如果他们有相同的问题ID,只抓一个答案。我尝试的是uniq或者不同。

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

你怎么能用同样的问题_id获得任何答案的答案?

1 个答案:

答案 0 :(得分:2)

试试这个:

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