mirror of https://github.com/rails/rails
Move records and loaded ivar up to load
We should set the `@records` and `@loaded` ivar inside `#load` rather than in `#exec_queries`. Since `#load` is running `#exec_queries` and `@records` can only be assigned if `loaded?` is true and `@loaded` can only be set if `loaded?` is true then `#load` should do the assignment. This is cleaner but also we came across this while working on an internal gem and realized that the ivar assignment was happening in the wrong place. Co-authored-by: Aaron Patterson <aaron.patterson@gmail.com>
This commit is contained in:
parent
19c5bd38f4
commit
847643a55a
|
@ -626,7 +626,10 @@ module ActiveRecord
|
||||||
#
|
#
|
||||||
# Post.where(published: true).load # => #<ActiveRecord::Relation>
|
# Post.where(published: true).load # => #<ActiveRecord::Relation>
|
||||||
def load(&block)
|
def load(&block)
|
||||||
exec_queries(&block) unless loaded?
|
unless loaded?
|
||||||
|
@records = exec_queries(&block)
|
||||||
|
@loaded = true
|
||||||
|
end
|
||||||
|
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
@ -809,7 +812,7 @@ module ActiveRecord
|
||||||
|
|
||||||
def exec_queries(&block)
|
def exec_queries(&block)
|
||||||
skip_query_cache_if_necessary do
|
skip_query_cache_if_necessary do
|
||||||
@records =
|
records =
|
||||||
if where_clause.contradiction?
|
if where_clause.contradiction?
|
||||||
[]
|
[]
|
||||||
elsif eager_loading?
|
elsif eager_loading?
|
||||||
|
@ -826,12 +829,11 @@ module ActiveRecord
|
||||||
klass.find_by_sql(arel, &block).freeze
|
klass.find_by_sql(arel, &block).freeze
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -16,10 +16,10 @@ module ActiveRecord
|
||||||
def exec_queries
|
def exec_queries
|
||||||
QueryRegistry.reset
|
QueryRegistry.reset
|
||||||
|
|
||||||
super.tap do
|
super.tap do |records|
|
||||||
if logger && warn_on_records_fetched_greater_than
|
if logger && warn_on_records_fetched_greater_than
|
||||||
if @records.length > 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(";")}"
|
logger.warn "Query fetched #{records.size} #{@klass} records: #{QueryRegistry.queries.join(";")}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue