0

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?})}
ravenUSMC
  • 495
  • 5
  • 23

2 Answers2

0

Try following, also ref this for how to use where condition

scope :search_for_received_orders, 
   ->{ joins(:item_instances)
       .where.not('item_instances.date_received': nil).distinct }


scope :search_for_not_received_orders, 
    ->{ joins(:item_instances)
        .where("item_instances.date_received IS NULL OR item_instances.date_received = ''"}).distinct }
Salil
  • 46,566
  • 21
  • 122
  • 156
  • Thank you Salil - however the search_for_not_received_orders works how I have it - I changed it to your better method though. It was the search_for_received_orders that is not fully working - getting orders that have item instances that have not been received. – ravenUSMC Sep 25 '20 at 18:29
0

Try this:

scope :received_orders, -> { includes(:item_instances).where.not(item_instances: {date_received: nil})}

scope :not_received_orders, -> { includes(:item_instances).where(item_instances: {date_received: nil})}

Received orders don't have any item instance with date_received nil. Not received orders have at least 1 item instance with date_received nil

catmal
  • 1,728
  • 1
  • 16
  • 32
  • Thank you catmal - however, are you sure that the received_orders is right? I know a bracket is left of the end but still getting errors when I use it - I still play around with it. Thank you again. – ravenUSMC Sep 25 '20 at 19:00
  • See my edit above - in the main area and again thank you for any help you can provide - this has been killing me all day! – ravenUSMC Sep 25 '20 at 19:07
  • Catmal, thank you so much! It works => really annoying on how close I was! Thank you again and have a great weekend! – ravenUSMC Sep 25 '20 at 19:28