Skip to content
Browse files

Merge pull request #14773 from eric-chahin/null_relation_fix

Changed the NullRelation so that when count is called with #group it wil...
Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/null_relation.rb
  • Loading branch information...
1 parent 112896b commit f5417fcf928bcc39341d261870c567a71bd4c923 @senny senny committed Apr 16, 2014
View
6 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*
+
* Do not quote uuid default value on `change_column`.
Fixes #14604.
View
12 activerecord/lib/active_record/null_relation.rb
@@ -43,15 +43,21 @@ def to_sql
end
def count(*)
- 0
+ calculate :count, nil
end
def sum(*)
0
end
- def calculate(_operation, _column_name, _options = {})
- nil
+ def calculate(operation, _column_name, _options = {})
+ # TODO: Remove _options argument as soon we remove support to
+ # activerecord-deprecated_finders.
+ if operation == :count
+ group_values.any? ? Hash.new : 0
+ else
+ nil
+ end
end
def exists?(_id = false)
View
11 activerecord/test/cases/relations_test.rb
@@ -14,6 +14,7 @@
require 'models/engine'
require 'models/tyre'
require 'models/minivan'
+require 'models/aircraft'
class RelationTest < ActiveRecord::TestCase
@@ -303,6 +304,16 @@ def test_null_relation_where_values_hash
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

0 comments on commit f5417fc

Please sign in to comment.
Something went wrong with that request. Please try again.