我需要找到所有子级状态为 1 的父级。
如果其中一个 child 不是status = 1;那么没有选择父级。
class Parent
has_many :children
end
class Child
status = [ 0, 1 ]
end
我已经试过了,但是没用。
Parent.left_outer_joins(:children).where("children.status = ?", 1)
它不起作用,因为我仍然得到 Parent with children status=0
最佳答案
我想你需要一个子查询:
Parent.where.not(id: Children.where.not(status: 1).select(:parent_id))
关于ruby-on-rails - rails : find all parents with all children having a particular attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55639108/