1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Changed the NullRelation so that when count is called with #group it will properly return an empty hash instead of zero. Fixes issue #14721

Conflicts:
	activerecord/CHANGELOG.md
This commit is contained in:
Eric Chahin 2014-04-16 02:27:26 -04:00
parent fe4b0eee05
commit 93f852569e
3 changed files with 19 additions and 2 deletions

View file

@ -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.

View file

@ -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

View file

@ -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