여기에 있습니다. 모델 질문에 여러 답변이 있습니다. 그래서 하나의 대답 만 갖는 모든 질문을 찾는 방법 (question.answers.count == 1). 어쨌든 쿼리하려면 ?레일스 쿼리에는 많은 연관이 있습니다. 데이터를 찾는 방법은 하나의 자식 만 있습니다.
1
A
답변
2
레일 Counter Cache
http://railscasts.com/episodes/23-counter-cache-column + 보통 Question.includes(:answers).where(:answers_count => 1)
나중에 질문과 답변 사이의 관계가 has_and_belongs_to_many 인 경우
0
트릭을해야 다음은이 외에도
Question.joins(:answers).select("count(answers_table_name.id) as answer_count, questions_table_name.id").
group("answers_table_name.question_id").having("answer_count=1")
+0
데이터베이스에 적은 쿼리를 만들 수 있습니다. 그리고 난 단지 대답 B가있는 질문을 찾아야 해. 내 솔루션은 answer_b.questions.where ("questions.id in (?)", one_answer_questions)와 같은 다른 쿼리를 추가한다. 여기에 두 단계가 있습니다. 하나의 명령문을 사용하여 쿼리 할 수 있습니까? –
정말 대단합니다 !!! –