鉴于Parent
有许多Child
s和status_id
属性,我想查找所有没有status_id:1
的孩子。换句话说,status_id
可能是nil
或不同的值。但我看到了一些有趣的现象:Rails ActiveRecord查找儿童,其中属性不是给定值
Parent.find(1).childs.where(status_id:nil)
=> #<ActiveRecord::AssociationRelation [#<Child id: 1, status_id: nil ...>]
Parent.find(1).childs.where.not(status_id:1)
=> #<ActiveRecord::AssociationRelation []>
你可以写为:'Parent.find(1).childs.where( “!STATUS_ID = 1”)'或 'Parent.find(1).childs.where(“status_id!=?”,1)' –
@KhanhPham即不工作....仍然得到'=>#' –
james