I'm working on a rails project that has two models - incoming_purchases and item_instances. An incoming purchase has many item_instances and an item_instance belongs to an incoming purchase. My problem is that I want to show all orders that have been received and to do this, each item_instance has to be received off the order. The item_instance model has a date_received column. So I can have an order, lets call 1.
Order 1: has two item instances where one of the items has been received: item 1: date_received: 9/15/20 item 2: date_recieved: nil
I need to show records where ever value of date received is filled in order for the order to be received. Thus, the example above would not be received. So far, I have a scope written that looks like this:
scope :search_for_received_orders, ->{ joins(:item_instances).where.not('item_instances.date_received': [nil] ).distinct }
This would get me the example above which would be a partially filled order. My code to see NOT received orders WORKS and is this:
scope :search_for_not_received_orders, ->{ joins(:item_instances).where('item_instances.date_received': [nil, ""] ).distinct }
Basically, I need to ensure for a received order that ALL Item Instances have a date in the date_received column. I cannot figure this out. Thank you for any help.
Error I'm getting with first response below:
/home/mcuddy/learn/inventory/src/inventory/app/models/incoming_purchase.rb:23: syntax error, unexpected '}', expecting => ...nces: {!date_received.present?})} ... ^
/home/mcuddy/learn/inventory/src/inventory/app/models/incoming_purchase.rb:36: syntax error, unexpected do (for lambda) item_instances.each do |i| ^~
/home/mcuddy/learn/inventory/src/inventory/app/models/incoming_purchase.rb:40: syntax error, unexpected end, expecting '}' end ^~~
scope :received_orders, -> { includes(:item_instances).where.not(item_instances: {!date_received.present?})}