[英]Rails 7 -- scope for associated records
对此有答案,但Rails 7 没有,我认为它增加了一种更好的方法来做到这一点:
Answer model: `belongs to :question`
Question Model: `has_many :answers`
然后,在问题Model中:
scope :answered, -> { where.associated(:answers) } #1
scope :unanswered, -> { includes(:answers).where(answers: {id: nil})} #2
我找到了第一个,where.associated(:answers)。 但我相信有更好的方法来完成第二个,这类似于第一个。 但我无法找到或让它工作。
从这里看起来好像语法是:
scope :unanswered, -> { where.missing(:answers) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.