Merge pull request #37926 from eileencodes/move-records-and-loaded-ivar-to-load

Move records and loaded ivar up to load
This commit is contained in:
Eileen M. Uchitelle 2019-12-10 15:14:11 -05:00 committed by GitHub
commit ab6f7f756f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 9 deletions

View File

@ -626,7 +626,10 @@ module ActiveRecord
#
# Post.where(published: true).load # => #<ActiveRecord::Relation>
def load(&block)
exec_queries(&block) unless loaded?
unless loaded?
@records = exec_queries(&block)
@loaded = true
end
self
end
@ -809,7 +812,7 @@ module ActiveRecord
def exec_queries(&block)
skip_query_cache_if_necessary do
@records =
records =
if where_clause.contradiction?
[]
elsif eager_loading?
@ -826,12 +829,11 @@ module ActiveRecord
klass.find_by_sql(arel, &block).freeze
end
preload_associations(@records) unless skip_preloading_value
preload_associations(records) unless skip_preloading_value
@records.each(&:readonly!) if readonly_value
records.each(&:readonly!) if readonly_value
@loaded = true
@records
records
end
end

View File

@ -16,10 +16,10 @@ module ActiveRecord
def exec_queries
QueryRegistry.reset
super.tap do
super.tap do |records|
if logger && warn_on_records_fetched_greater_than
if @records.length > warn_on_records_fetched_greater_than
logger.warn "Query fetched #{@records.size} #{@klass} records: #{QueryRegistry.queries.join(";")}"
if records.length > warn_on_records_fetched_greater_than
logger.warn "Query fetched #{records.size} #{@klass} records: #{QueryRegistry.queries.join(";")}"
end
end
end