diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index e044099b72ffd30eb12b021f1b638d945f4f9462..1b746b46b72a6557a8843a6f1612f6b9f95f0800 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -28,6 +28,8 @@ module Associations # is computed directly through SQL and does not trigger by itself the # instantiation of the actual post records. class CollectionProxy < Relation + delegate *ActiveRecord::Calculations.public_instance_methods, to: :scope + def initialize(association) #:nodoc: @association = association super association.klass, association.klass.arel_table @@ -965,10 +967,6 @@ def reload proxy_association.reload self end - - def pluck(column_names) - scope.pluck(column_names) - end end end end diff --git a/activerecord/lib/active_record/null_relation.rb b/activerecord/lib/active_record/null_relation.rb index f8f7a6c9769618179a2b5b31f5828abccc1139ae..711fc8b883ca189d5f948c9b7dd6a866ba61054f 100644 --- a/activerecord/lib/active_record/null_relation.rb +++ b/activerecord/lib/active_record/null_relation.rb @@ -50,6 +50,10 @@ def count(*) 0 end + def sum(*) + 0 + end + def calculate(_operation, _column_name, _options = {}) nil end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index baf07f352e35294b3925c7b1b2eb090590a615c1..aae93bb9d0bb7a947290f9fd9374f96dfcd08e25 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1656,6 +1656,7 @@ def test_collection_association_with_private_kernel_method assert_equal [], post.comments assert_equal [], post.comments.where(body: 'omg') assert_equal [], post.comments.pluck(:body) + assert_equal 0, post.comments.sum(:id) end end end