提交 edd94cee 编写于 作者: J Jon Leighton

Delegate all calculations to the scope.

So that the scope may be a NullRelation and return a result without
executing a query.

Fixes #7928
上级 11b846ee
...@@ -28,6 +28,8 @@ module Associations ...@@ -28,6 +28,8 @@ module Associations
# is computed directly through SQL and does not trigger by itself the # is computed directly through SQL and does not trigger by itself the
# instantiation of the actual post records. # instantiation of the actual post records.
class CollectionProxy < Relation class CollectionProxy < Relation
delegate *ActiveRecord::Calculations.public_instance_methods, to: :scope
def initialize(association) #:nodoc: def initialize(association) #:nodoc:
@association = association @association = association
super association.klass, association.klass.arel_table super association.klass, association.klass.arel_table
...@@ -965,10 +967,6 @@ def reload ...@@ -965,10 +967,6 @@ def reload
proxy_association.reload proxy_association.reload
self self
end end
def pluck(column_names)
scope.pluck(column_names)
end
end end
end end
end end
...@@ -50,6 +50,10 @@ def count(*) ...@@ -50,6 +50,10 @@ def count(*)
0 0
end end
def sum(*)
0
end
def calculate(_operation, _column_name, _options = {}) def calculate(_operation, _column_name, _options = {})
nil nil
end end
......
...@@ -1656,6 +1656,7 @@ def test_collection_association_with_private_kernel_method ...@@ -1656,6 +1656,7 @@ def test_collection_association_with_private_kernel_method
assert_equal [], post.comments assert_equal [], post.comments
assert_equal [], post.comments.where(body: 'omg') assert_equal [], post.comments.where(body: 'omg')
assert_equal [], post.comments.pluck(:body) assert_equal [], post.comments.pluck(:body)
assert_equal 0, post.comments.sum(:id)
end end
end end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册