简体   繁体   中英

Rails multiple condition query

I have a LineItem class which belongs to an order. An order has many line items. An order has two attributes, service_category and state. I need to get all orders that have the below conditions. Currently I am using the below 2 queries but I really want to just use 1 query, is this possible?

LineItem.includes(:order).where(
   menu_item_id:menu_item_id,
   orders:{
     service_category: "delivery",
     state:["pending", "open"]
    }
  )  

LineItem.includes(:order).where(
   menu_item_id:menu_item_id,
   orders:{
     service_category: "walkup",
     state:["pending", "closed"]
    }
  ) 

I am not sure of the exact syntax since I don't have the models set up, but shouldn't you be able to include both states separated by an OR (||)? Have you tried something like this?

LineItem.includes(:order).where(
   menu_item_id:menu_item_id,
   orders:{
     (service_category: "delivery",
     state:["pending", "open"])
     ||
     (service_category: "walkup",
     state:["pending", "closed"])
    }
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM