diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0e9badab42..0d8f209ce2 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,9 +13,9 @@ class ApplicationController < ActionController::Base sidebar_distributors_limit = 5 #set false to disable TODO: move to app config sidebar_suppliers_limit = 5 @sidebar_distributors = Enterprise.is_distributor.with_distributed_active_products_on_hand.by_name.limit(sidebar_distributors_limit) - @total_distributors = Enterprise.is_distributor.with_distributed_active_products_on_hand.by_name.count(:distinct => true) + @total_distributors = Enterprise.is_distributor.with_distributed_active_products_on_hand.by_name.distinct_count @sidebar_suppliers = Enterprise.is_primary_producer.with_supplied_active_products_on_hand.limit(sidebar_suppliers_limit) - @total_suppliers = Enterprise.is_primary_producer.with_supplied_active_products_on_hand.count(:distinct => true) + @total_suppliers = Enterprise.is_primary_producer.with_supplied_active_products_on_hand.distinct_count end # All render calls within the block will be performed with the specified format diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 29002444f7..0ea6afa580 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -18,10 +18,14 @@ class Enterprise < ActiveRecord::Base scope :is_primary_producer, where(:is_primary_producer => true) scope :is_distributor, where(:is_distributor => true) scope :with_supplied_active_products_on_hand, lambda { - joins(:supplied_products).where('spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0', Time.now).select('distinct(enterprises.*)') } + joins(:supplied_products).where('spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0', Time.now).uniq } scope :with_distributed_active_products_on_hand, lambda { - joins(:distributed_products).where('spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0', Time.now).select('distinct(enterprises.*)') } + joins(:distributed_products).where('spree_products.deleted_at IS NULL AND spree_products.available_on <= ? AND spree_products.count_on_hand > 0', Time.now).uniq } + # Force a distinct count to work around relation count issue https://github.com/rails/rails/issues/5554 + def self.distinct_count + count(distinct: true) + end def has_supplied_products_on_hand? self.supplied_products.where('count_on_hand > 0').present? diff --git a/spec/models/enterprises_spec.rb b/spec/models/enterprises_spec.rb index 41e02327d4..8e8ce97dad 100644 --- a/spec/models/enterprises_spec.rb +++ b/spec/models/enterprises_spec.rb @@ -29,6 +29,7 @@ describe Enterprise do create(:product, :distributors => [d3], :on_hand => 0) Enterprise.with_distributed_active_products_on_hand.sort.should == [d1, d2] + Enterprise.with_distributed_active_products_on_hand.distinct_count.should == 2 end it "returns suppliers with products in stock" do @@ -37,10 +38,12 @@ describe Enterprise do d3 = create(:supplier_enterprise) d4 = create(:supplier_enterprise) create(:product, :supplier => d1, :on_hand => 5) + create(:product, :supplier => d1, :on_hand => 5) create(:product, :supplier => d2, :on_hand => 5, :available_on => 1.week.from_now) create(:product, :supplier => d3, :on_hand => 0) # supplier with no products, supplier with product out of stock, supplier with product thats unavailable, supplier with active product on hand Enterprise.with_supplied_active_products_on_hand.sort.should == [d1] + Enterprise.with_supplied_active_products_on_hand.distinct_count.should == 1 end end