diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-01-28 16:01:12 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-01-28 16:01:12 -0700 |
commit | 74c2961bd864f633c79c03e62c2cb142642201c5 (patch) | |
tree | 442b1f37ab6aa110d3d2a21a14c65b3a8dc9b327 | |
parent | 9e42cf019f2417473e7dcbfcb885709fa2709f89 (diff) | |
download | rails-74c2961bd864f633c79c03e62c2cb142642201c5.tar.gz rails-74c2961bd864f633c79c03e62c2cb142642201c5.tar.bz2 rails-74c2961bd864f633c79c03e62c2cb142642201c5.zip |
Don't error when grouped calculations return 0 records
Fixes #18717
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/calculations_test.rb | 5 |
2 files changed, 6 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 83745bc169..c3c4d7f1ce 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -304,7 +304,7 @@ module ActiveRecord if association key_ids = calculated_data.collect { |row| row[group_aliases.first] } - key_records = association.klass.base_class.find(key_ids) + key_records = association.klass.base_class.where(association.klass.base_class.primary_key => key_ids) key_records = Hash[key_records.map { |r| [r.id, r] }] end diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 299217214e..f47568f2f5 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -631,4 +631,9 @@ class CalculationsTest < ActiveRecord::TestCase assert_equal({ "has trinket" => part.id }, ShipPart.joins(:trinkets).group("ship_parts.name").sum(:id)) end + + def test_calculation_grouped_by_association_doesnt_error_when_no_records_have_association + Client.update_all(client_of: nil) + assert_equal({ nil => Client.count }, Client.group(:firm).count) + end end |