mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
pluck
should use records
(load_target
) when loaded?
is true
This commit is contained in:
parent
5b469da6ec
commit
3a8a9979ac
3 changed files with 20 additions and 2 deletions
|
@ -28,7 +28,6 @@ module ActiveRecord
|
||||||
# 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 - [:count]), to: :scope)
|
|
||||||
delegate :exists?, :update_all, :arel, to: :scope
|
delegate :exists?, :update_all, :arel, to: :scope
|
||||||
|
|
||||||
def initialize(klass, association) #:nodoc:
|
def initialize(klass, association) #:nodoc:
|
||||||
|
@ -738,6 +737,14 @@ module ActiveRecord
|
||||||
@association.count(column_name, &block)
|
@association.count(column_name, &block)
|
||||||
end
|
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,
|
# 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>.
|
# it executes a <tt>SELECT COUNT(*)</tt> query. Else it calls <tt>collection.size</tt>.
|
||||||
#
|
#
|
||||||
|
@ -1073,6 +1080,10 @@ module ActiveRecord
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def null_scope?
|
||||||
|
@association.null_scope?
|
||||||
|
end
|
||||||
|
|
||||||
def exec_queries
|
def exec_queries
|
||||||
load_target
|
load_target
|
||||||
end
|
end
|
||||||
|
|
|
@ -160,7 +160,7 @@ module ActiveRecord
|
||||||
#
|
#
|
||||||
def pluck(*column_names)
|
def pluck(*column_names)
|
||||||
if loaded? && (column_names.map(&:to_s) - @klass.attribute_names - @klass.attribute_aliases.keys).empty?
|
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
|
end
|
||||||
|
|
||||||
if has_include?(column_names.first)
|
if has_include?(column_names.first)
|
||||||
|
|
|
@ -258,6 +258,13 @@ class AssociationProxyTest < ActiveRecord::TestCase
|
||||||
assert_no_queries { david.posts.first! }
|
assert_no_queries { david.posts.first! }
|
||||||
end
|
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
|
def test_reset_unloads_target
|
||||||
david = authors(:david)
|
david = authors(:david)
|
||||||
david.posts.reload
|
david.posts.reload
|
||||||
|
|
Loading…
Reference in a new issue