From 977344e279388af1c13b8a86d85b0c76745d6acb Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 8 Mar 2013 16:31:54 +1100 Subject: [PATCH] Rewrite scope with outer join --- app/models/enterprise.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index af56a6c638..9f58cc2368 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -26,7 +26,9 @@ class Enterprise < ActiveRecord::Base merge(OrderCycle.active). select('DISTINCT enterprises.*') - scope :active_distributors, joins(:distributed_products) + scope :with_distributed_products_outer, joins('LEFT OUTER JOIN product_distributions ON product_distributions.distributor_id = enterprises.id') + + scope :active_distributors, with_distributed_products_outer.where('product_distributions.product_id IS NOT NULL') def has_supplied_products_on_hand?