Ruby on rails Rails活动记录.where语句优化
我正在建立一个电子商务网站,希望使用活动记录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”语句。我希望它同时显示已发货和
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