Rails where.not, include record having nil attribute



我有一个具有属性 skip_reasonOrder模型。我想列出除fulllocked以外的所有具有skip_reason的顺序。我正在使用where.not,但它不会在skip_reason列上返回具有nil的订单。

我想到了以下条件,

> orders.pluck(:skip_reason)
=> ["locked", nil, nil, "unavailable", nil, nil, "locked", nil, nil, "full"]
> orders.where.not(skip_reason: ["full", "locked"]).pluck(:skip_reason)
=> ["unavailable"]

那么,如何列出skip_reason列中具有nil值的订单?任何建议都将不胜感激。

在Rails 4中尝试了此操作,并且似乎可以使用。对你还可以吗?否则,我认为您应该使用RAW SQL作为WHERE子句

orders.where(skip_reason: [nil, "unavailable"])

最新更新