diff options
author | Ben Woosley <ben.woosley@gmail.com> | 2013-05-10 20:45:09 +0200 |
---|---|---|
committer | Ben Woosley <ben.woosley@gmail.com> | 2013-05-10 20:45:09 +0200 |
commit | a2e607e1055dcede27970ccd7f5b89a1ddee8c32 (patch) | |
tree | 595e27bae9bb51850f61e8be53f083fd82cce52b /activerecord | |
parent | 86cc141ed5fd51ec752c956febfa436d1c816532 (diff) | |
download | rails-a2e607e1055dcede27970ccd7f5b89a1ddee8c32.tar.gz rails-a2e607e1055dcede27970ccd7f5b89a1ddee8c32.tar.bz2 rails-a2e607e1055dcede27970ccd7f5b89a1ddee8c32.zip |
Make NullRelation a bit more like a real relation by returning 0 for #calculate(:count)
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/null_relation.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 5 |
2 files changed, 8 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/null_relation.rb b/activerecord/lib/active_record/null_relation.rb index 5d801fa705..d166f0dd66 100644 --- a/activerecord/lib/active_record/null_relation.rb +++ b/activerecord/lib/active_record/null_relation.rb @@ -55,7 +55,11 @@ module ActiveRecord end def calculate(_operation, _column_name, _options = {}) - nil + if _operation == :count + 0 + else + nil + end end def exists?(_id = false) diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index cf6af4e8f4..2ab5231458 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -278,8 +278,9 @@ class RelationTest < ActiveRecord::TestCase def test_null_relation_calculations_methods assert_no_queries do - assert_equal 0, Developer.none.count - assert_equal nil, Developer.none.calculate(:average, 'salary') + assert_equal 0, Developer.none.count + assert_equal 0, Developer.none.calculate(:count, nil, {}) + assert_equal nil, Developer.none.calculate(:average, 'salary') end end |