aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2016-08-17 00:35:25 -0300
committerGitHub <noreply@github.com>2016-08-17 00:35:25 -0300
commitf4b32cdc6c5b00c0ef6b5eba831234a8155768fd (patch)
treed30a91392e00f8297c13bdf79bfb25052ad0ec48
parent64bfc5b7902c9bcb282d7d6d55809f271a3544fd (diff)
parent3a8a9979acab6a6f4c6d759521c052fe2ac46d2b (diff)
downloadrails-f4b32cdc6c5b00c0ef6b5eba831234a8155768fd.tar.gz
rails-f4b32cdc6c5b00c0ef6b5eba831234a8155768fd.tar.bz2
rails-f4b32cdc6c5b00c0ef6b5eba831234a8155768fd.zip
Merge pull request #25976 from kamipo/pluck_uses_loaded_target
`pluck` should use `records` (`load_target`) when `loaded?` is true
-rw-r--r--activerecord/lib/active_record/associations/collection_proxy.rb13
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb2
-rw-r--r--activerecord/test/cases/associations_test.rb7
3 files changed, 20 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb
index a3aef07746..36f6c1b9c3 100644
--- a/activerecord/lib/active_record/associations/collection_proxy.rb
+++ b/activerecord/lib/active_record/associations/collection_proxy.rb
@@ -28,7 +28,6 @@ module ActiveRecord
# 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 - [:count]), to: :scope)
delegate :exists?, :update_all, :arel, to: :scope
def initialize(klass, association) #:nodoc:
@@ -738,6 +737,14 @@ module ActiveRecord
@association.count(column_name, &block)
end
+ def calculate(operation, column_name)
+ null_scope? ? scope.calculate(operation, column_name) : super
+ end
+
+ def pluck(*column_names)
+ null_scope? ? scope.pluck(*column_names) : super
+ end
+
# Returns the size of the collection. If the collection hasn't been loaded,
# it executes a <tt>SELECT COUNT(*)</tt> query. Else it calls <tt>collection.size</tt>.
#
@@ -1073,6 +1080,10 @@ module ActiveRecord
private
+ def null_scope?
+ @association.null_scope?
+ end
+
def exec_queries
load_target
end
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index a4962879ab..b569abc7a8 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -163,7 +163,7 @@ module ActiveRecord
#
def pluck(*column_names)
if loaded? && (column_names.map(&:to_s) - @klass.attribute_names - @klass.attribute_aliases.keys).empty?
- return @records.pluck(*column_names)
+ return records.pluck(*column_names)
end
if has_include?(column_names.first)
diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb
index f29a968c07..5222703570 100644
--- a/activerecord/test/cases/associations_test.rb
+++ b/activerecord/test/cases/associations_test.rb
@@ -256,6 +256,13 @@ class AssociationProxyTest < ActiveRecord::TestCase
assert_no_queries { david.posts.first! }
end
+ def test_pluck_uses_loaded_target
+ david = authors(:david)
+ assert_equal david.posts.pluck(:title), david.posts.load.pluck(:title)
+ assert david.posts.loaded?
+ assert_no_queries { david.posts.pluck(:title) }
+ end
+
def test_reset_unloads_target
david = authors(:david)
david.posts.reload