Ruby on rails Rails 6.1自引用缺少很多内容

Ruby on rails Rails 6.1自引用缺少很多内容,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,查找孤立记录的missing方法,但我很难将其应用于自引用中,因为它有很多 以这种模式为例: 类项{approved},类名称:“项”,外键::父项id 范围:批准,->{where.not(批准时间:nil)} #这不起作用: 作用域:无子项,->{where.missing(:子项)} #这也不起作用: 范围:无批准的子项,->{where.missing(:批准的子项)} 结束 我想找到所有没有子项的项 我如何让无子女项目和无子女项目范围发挥作用?看看,新方法似乎只是旧方法(即旧方法)的

查找孤立记录的
missing
方法,但我很难将其应用于自引用
中,因为它有很多

以这种模式为例:

类项{approved},类名称:“项”,外键::父项id
范围:批准,->{where.not(批准时间:nil)}
#这不起作用:
作用域:无子项,->{where.missing(:子项)}
#这也不起作用:
范围:无批准的子项,->{where.missing(:批准的子项)}
结束
我想找到所有没有子项的

我如何让
无子女项目
无子女项目
范围发挥作用?

看看,新方法似乎只是旧方法(即旧方法)的糖衣,但只是它的简单形式。通过自我参照关系,您可以

.left_joins(child_items: :items)

但是
缺失中似乎不支持这一点。因此,您可能无法使用
missing
进行此操作。请改为使用。

很抱歉太密集了,但是左连接的完整代码是什么
left\u joins(child\u items::items)。其中(child\u items:{id:nil})
引发了一个
ActiveRecord::ConfigurationError
@sethherr:hm,我想这应该是有效的。我有时间时必须在本地试用。@sethherr您应该能够使用
where.not(id:approved.select(:parent\u item\u id))