There are answers for this, but not for Rails 7 which I think added a better way to do this:
Answer model: `belongs to :question`
Question Model: `has_many :answers`
Then, in Question Model:
scope :answered, -> { where.associated(:answers) } #1
scope :unanswered, -> { includes(:answers).where(answers: {id: nil})} #2
I found the first one, where.associated(:answers). But I believe theres a better way to do the second one, that is analogous to the first one. But I can't find or get that to work.
from here it looks as if the syntax is:
scope :unanswered, -> { where.missing(:answers) }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.