这是有答案的,但Rails 7没有答案,我认为Rails 7增加了一个更好的方法来实现这一点:
Answer model: `belongs to :question`
Question Model: `has_many :answers`然后,在问题模型中:
scope :answered, -> { where.associated(:answers) } #1
scope :unanswered, -> { includes(:answers).where(answers: {id: nil})} #2我找到了第一个,where.associated(:answers)。但我相信有一个更好的方法来做第二个,那是类似于第一个。但我找不到也不能让它起作用。
发布于 2022-10-01 15:59:48
从这里看,似乎语法是:
scope :unanswered, -> { where.missing(:answers) }https://stackoverflow.com/questions/73919685
复制相似问题