diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2016-07-28 23:31:09 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2016-08-04 09:13:13 +0900 |
commit | 3a8a9979acab6a6f4c6d759521c052fe2ac46d2b (patch) | |
tree | 1ab380c8ca111682cb6dc12687fc1985ee53b93e /activerecord/lib | |
parent | 5b469da6ec482414c5f59762ae8e82de7e07c365 (diff) | |
download | rails-3a8a9979acab6a6f4c6d759521c052fe2ac46d2b.tar.gz rails-3a8a9979acab6a6f4c6d759521c052fe2ac46d2b.tar.bz2 rails-3a8a9979acab6a6f4c6d759521c052fe2ac46d2b.zip |
`pluck` should use `records` (`load_target`) when `loaded?` is true
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_proxy.rb | 13 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 2 |
2 files changed, 13 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 806a905323..7c96341f40 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 a97b71815a..24b1131752 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -160,7 +160,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) |