Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails where.not,包括具有nil属性的记录_Ruby On Rails_Ruby_Ruby On Rails 4_Activerecord - Fatal编程技术网

Ruby on rails Rails where.not,包括具有nil属性的记录

Ruby on rails Rails where.not,包括具有nil属性的记录,ruby-on-rails,ruby,ruby-on-rails-4,activerecord,Ruby On Rails,Ruby,Ruby On Rails 4,Activerecord,我有一个订单模型,它有一个属性skip\u reason。我想列出除full和locked之外的所有具有skip\u原因的订单。 我使用的是where.not,但它不会返回skip\u reason列上的nil订单 我提出了以下条件, > orders.pluck(:skip_reason) => ["locked", nil, nil, "unavailable", nil, nil, "locked", nil, nil, "full"] > orders.where.n

我有一个
订单
模型,它有一个属性
skip\u reason
。我想列出除
full
locked
之外的所有具有
skip\u原因的订单。
我使用的是
where.not
,但它不会返回
skip\u 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中尝试过这一点,它似乎很有效。对你来说可以吗?否则,我认为应该使用原始SQL作为where子句

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

在rails 4中尝试过这一点,似乎效果不错。对你来说可以吗?否则,我认为应该使用原始SQL作为where子句

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

谢谢,但在这些情况下我可能有其他数据。谢谢,但在这些情况下我可能有其他数据。您可以添加上述查询的SQL输出吗?因为您使用的是@Ursus指出的rails 4,所以您必须使用原始SQL作为
订单。其中(“orders.skip\u reason为NULL或orders.skip\u reason不在('full','locked')”
)。Rails 5确实提供了一个
,它看起来像是
orders.where.not(skip_reason:[“full”,“locked”])或(orders.where(skip_reason:nil))
您可以添加上面查询的SQL输出吗?因为您使用的是@Ursus指出的Rails 4,所以您必须使用原始SQL作为
orders.where(“orders.skip_reason为NULL或orders.skip_reason NOT IN('full','locked')”
。Rails 5提供了一个
,看起来像
订单。where.NOT(skip_reason:[“full”,“locked”])或(orders.where(skip_reason:nil))