Reputation:
I'm trying to figure out a better way to have one query here. I want to be able to send something to last where statement a wildcard so I can select all vendors. Right now if i don't include that line it doesn't filter by vendor so I essentially get all the purchase requests.
Any thoughts of a cleaner way to do these sorts of queries?
if @vendor == "0" #checks for vendor
@purchase_requests = PurchaseRequest.includes(:purchase_order)
.where(:created_at => @date_start..@date_end)
.where(:total_cost => @cost_beginning..@cost_end)
else
@purchase_requests = PurchaseRequest.includes(:purchase_order)
.where(:created_at => @date_start..@date_end)
.where(:total_cost => @cost_beginning..@cost_end)
.where("purchaseorder.VendorRef_ListID = ?", @vendor)
end
Upvotes: 0
Views: 50
Reputation: 3268
there must be some better solution, but try this
@purchase_requests = PurchaseRequest.includes(:purchase_order).where(created_at: @date_start..@date_end, total_cost: @cost_beginning..@cost_end)
@purchase_requests = @purchase_requests.where("purchaseorder.VendorRef_ListID = ?", @vendor) unless @vendor == "0"
Upvotes: 2
Reputation: 1989
Here is a simplified version:
@purchase_requests = PurchaseRequest
.includes(:purchase_order)
.where(created_at: @date_start..@date_end)
.where(total_cost: @cost_beginning..@cost_end)
@purchase_requests = @purchase_requests.where('purchase_orders.VendorRef_ListID = ?', @vendor) unless @vendor == '0'
Upvotes: 0