Skip to content
This repository has been archived by the owner on Nov 10, 2024. It is now read-only.

Latest commit

 

History

History
46 lines (35 loc) · 1.26 KB

rails-question-getting-x-where-there-are-no-y.org

File metadata and controls

46 lines (35 loc) · 1.26 KB

[Rails Question] Getting X where there are no Y

WARNING: This is old and likely obsolete.

There was a question recently in the #RubyOnRails IRC channel on freenode: “How can I retrieve all the questions that don’t have answers?”. This Rails coding example shows the answer.

Two Models

  • Question
  • Answer

Associations

  • Question has_many Answers
  • Answer belongs_to Question

Finding Questions with No Answers:

Question.includes(:answers).where(answers: {id: nil})

Converse: Finding All Questions that have Answers:

Question.includes(:answers).where.not(answers: {id: nil})