From be667699996c1ad96ea16a11699539bef88a682a Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 5 Aug 2015 11:05:29 +1000 Subject: [PATCH] SELECT DISTINCT results in inconsistent #count value. Work around this with to_a. --- app/models/order_cycle.rb | 3 ++- lib/open_food_network/enterprise_fee_calculator.rb | 2 +- spec/factories.rb | 2 +- spec/models/order_cycle_spec.rb | 4 ++++ 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index ae58f1335a..bc06594b96 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -136,7 +136,8 @@ class OrderCycle < ActiveRecord::Base joins(:exchanges). merge(Exchange.in_order_cycle(self)). not_deleted. - select('DISTINCT spree_variants.*') + select('DISTINCT spree_variants.*'). + to_a # http://stackoverflow.com/q/15110166 end def distributed_variants diff --git a/lib/open_food_network/enterprise_fee_calculator.rb b/lib/open_food_network/enterprise_fee_calculator.rb index a8f56122f0..d65a80c0dc 100644 --- a/lib/open_food_network/enterprise_fee_calculator.rb +++ b/lib/open_food_network/enterprise_fee_calculator.rb @@ -89,7 +89,7 @@ module OpenFoodNetwork def load_coordinator_fees @order_cycle.coordinator_fees.per_item.each do |enterprise_fee| - @order_cycle.variants.pluck(:id).each do |variant_id| + @order_cycle.variants.map(&:id).each do |variant_id| @indexed_enterprise_fees[variant_id] ||= [] @indexed_enterprise_fees[variant_id] << enterprise_fee end diff --git a/spec/factories.rb b/spec/factories.rb index b037eac843..169d2d5c02 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -23,7 +23,7 @@ FactoryGirl.define do ExchangeFee.create!(exchange: ex2, enterprise_fee: create(:enterprise_fee, enterprise: ex2.sender)) - #Distributors + # Distributors distributor1 = create(:distributor_enterprise) distributor2 = create(:distributor_enterprise) diff --git a/spec/models/order_cycle_spec.rb b/spec/models/order_cycle_spec.rb index c05d3d00d5..f87872a44d 100644 --- a/spec/models/order_cycle_spec.rb +++ b/spec/models/order_cycle_spec.rb @@ -218,6 +218,10 @@ describe OrderCycle do @oc.variants.should match_array [@p0.master, @p1.master, @p2.master, @p2_v] end + it "returns the correct count of variants" do + @oc.variants.count.should == 4 + end + it "reports on the variants distributed" do @oc.distributed_variants.should match_array [@p1.master, @p2.master, @p2_v] end