Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.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语句优化_Ruby On Rails_Rails Activerecord_Where - Fatal编程技术网

Ruby on rails Rails活动记录.where语句优化

Ruby on rails Rails活动记录.where语句优化,ruby-on-rails,rails-activerecord,where,Ruby On Rails,Rails Activerecord,Where,我正在建立一个电子商务网站,希望使用活动记录where语句来查找范围为特定供应商和特定装运状态的装运。以下是我现在拥有的: Spree::Shipment.where("stock_location_id = ? and "state = ?", spree_current_user.supplier.stock_locations.first.stock_location_id, 'shipped' || 'ready') 我发现这只会导致返回“shipped”语句。我希望它同时显示已发货和

我正在建立一个电子商务网站,希望使用活动记录where语句来查找范围为特定供应商和特定装运状态的装运。以下是我现在拥有的:

Spree::Shipment.where("stock_location_id = ? and "state = ?", spree_current_user.supplier.stock_locations.first.stock_location_id, 'shipped' || 'ready')
我发现这只会导致返回“shipped”语句。我希望它同时显示已发货和已准备发货。到目前为止,我只能让它显示一个或另一个,这取决于我是将“shipped”还是“ready”放在查询的第一位

我猜我把OR运算符(| |)放错了位置,即使没有错误。有人能告诉我在where语句中放置或运算符的正确方法吗

谢谢

  • 布兰登

我在写问题的时候找到了答案。我想与大家分享答案,以防其他人遇到这个问题。这似乎起到了作用:

Spree::Shipment.where("stock_location_id = ? and (state = ? or state = ?)", spree_current_user.supplier.stock_locations.first.id, 'shipped', 'ready')
我在控制台中对它进行了测试,它返回了这个SQL输出,发货处于“就绪”和“已发货”状态:

Spree::Shipment Load (0.6ms)  SELECT  "spree_shipments".* FROM "spree_shipments"  WHERE (stock_location_id = 8 and (state = 'shipped' or state = 'ready')) LIMIT 15 OFFSET 0
希望这能帮助别人。另外,如果你注意到这句话看起来很奇怪或低效,我真的很想知道


谢谢

哇,反应真快。这看起来比我的答案更清楚。我要试试看!
Spree::Shipment Load (0.6ms)  SELECT  "spree_shipments".* FROM "spree_shipments"  WHERE (stock_location_id = 8 and (state = 'shipped' or state = 'ready')) LIMIT 15 OFFSET 0