aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-04-16 17:03:24 +0200
committerYves Senn <yves.senn@gmail.com>2014-04-16 17:03:24 +0200
commitc1dc6470cbb2708b4bf1bf3bc0681e8f811341b2 (patch)
tree491f78a36dabb65b5cdf8bfa8316f29b2c7a85d4 /activerecord
parent973a45230ab5ba0e096585ecd1403a13569a1348 (diff)
parent93f852569efc4db62e2fed75b2c0bb1866f7065b (diff)
downloadrails-c1dc6470cbb2708b4bf1bf3bc0681e8f811341b2.tar.gz
rails-c1dc6470cbb2708b4bf1bf3bc0681e8f811341b2.tar.bz2
rails-c1dc6470cbb2708b4bf1bf3bc0681e8f811341b2.zip
Merge pull request #14773 from eric-chahin/null_relation_fix
Changed the NullRelation so that when count is called with #group it wil...
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/null_relation.rb4
-rw-r--r--activerecord/test/cases/relations_test.rb11
3 files changed, 19 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 0a12049e5d..57a083c1df 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* Fixed a problem where count used with a grouping was not returning a Hash.
+
+ Fixes #14721.
+
+ *Eric Chahin*
+
* `sanitize_sql_like` helper method to escape a string for safe use in a SQL
LIKE statement.
diff --git a/activerecord/lib/active_record/null_relation.rb b/activerecord/lib/active_record/null_relation.rb
index 5b255c3fe5..05d0c41678 100644
--- a/activerecord/lib/active_record/null_relation.rb
+++ b/activerecord/lib/active_record/null_relation.rb
@@ -43,7 +43,7 @@ module ActiveRecord
end
def count(*)
- 0
+ calculate :count, nil
end
def sum(*)
@@ -54,7 +54,7 @@ module ActiveRecord
# TODO: Remove _options argument as soon we remove support to
# activerecord-deprecated_finders.
if operation == :count
- 0
+ group_values.any? ? Hash.new : 0
else
nil
end
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index ddfcaaf986..d054dfa25a 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -14,6 +14,7 @@ require 'models/car'
require 'models/engine'
require 'models/tyre'
require 'models/minivan'
+require 'models/aircraft'
class RelationTest < ActiveRecord::TestCase
@@ -365,6 +366,16 @@ class RelationTest < ActiveRecord::TestCase
assert_equal({ 'salary' => 100_000 }, Developer.none.where(salary: 100_000).where_values_hash)
end
+
+ def test_null_relation_count
+ ac = Aircraft.new
+ assert_equal Hash.new, ac.engines.group(:id).count
+ assert_equal 0, ac.engines.count
+ ac.save
+ assert_equal Hash.new, ac.engines.group(:id).count
+ assert_equal 0, ac.engines.count
+ end
+
def test_joins_with_nil_argument
assert_nothing_raised { DependentFirm.joins(nil).first }
end