From 017916b1933e4d6cd0b09acbca6bb8c87d7b1eb4 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 5 Jan 2017 12:17:24 +1100 Subject: [PATCH] Enterprise distributing_products scope uses INNER JOINS instead of OUTER JOINS --- app/models/enterprise.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 840477185f8..e9acbd51f62 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -163,10 +163,18 @@ class Enterprise < ActiveRecord::Base } scope :distributing_products, lambda { |products| - with_distributed_products_outer.with_order_cycles_and_exchange_variants_outer. - where('product_distributions.product_id IN (?) OR spree_variants.product_id IN (?)', products, products). - select('DISTINCT enterprises.*') + # TODO: remove this when we pull out product distributions + pds = joins("INNER JOIN product_distributions ON product_distributions.distributor_id = enterprises.id"). + where("product_distributions.product_id IN (?)", products).select('DISTINCT enterprises.id') + + exs = joins("INNER JOIN exchanges ON (exchanges.receiver_id = enterprises.id AND exchanges.incoming = 'f')"). + joins('INNER JOIN exchange_variants ON (exchange_variants.exchange_id = exchanges.id)'). + joins('INNER JOIN spree_variants ON (spree_variants.id = exchange_variants.variant_id)'). + where('spree_variants.product_id IN (?)', products).select('DISTINCT enterprises.id') + + where(id: pds | exs) } + scope :managed_by, lambda { |user| if user.has_spree_role?('admin') scoped