aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2016-08-31 08:54:38 -0400
committerSean Griffin <sean@seantheprogrammer.com>2016-08-31 09:26:25 -0400
commitcaa178c178468f7adcc5f4c597280e6362d9e175 (patch)
tree92e31877f28485d694d5e55bc315ef36b924ca37 /activerecord/lib/active_record/relation.rb
parent0ca595ea6e3a9523b7902b05305437054de67e43 (diff)
downloadrails-caa178c178468f7adcc5f4c597280e6362d9e175.tar.gz
rails-caa178c178468f7adcc5f4c597280e6362d9e175.tar.bz2
rails-caa178c178468f7adcc5f4c597280e6362d9e175.zip
Ensure that inverse associations are set before running callbacks
If a parent association was accessed in an `after_find` or `after_initialize` callback, it would always end up loading the association, and then immediately overwriting the association we just loaded. If this occurred in a way that the parent's `current_scope` was set to eager load the child, this would result in an infinite loop and eventually overflow the stack. For records that are created with `.new`, we have a mechanism to perform an action before the callbacks are run. I've introduced the same code path for records created with `instantiate`, and updated all code which sets inverse instances on newly loaded associations to use this block instead. Fixes #26320.
Diffstat (limited to 'activerecord/lib/active_record/relation.rb')
-rw-r--r--activerecord/lib/active_record/relation.rb8
1 files changed, 4 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index d7de1032b6..6d571cf026 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -562,8 +562,8 @@ module ActiveRecord
# return value is the relation itself, not the records.
#
# Post.where(published: true).load # => #<ActiveRecord::Relation>
- def load
- exec_queries unless loaded?
+ def load(&block)
+ exec_queries(&block) unless loaded?
self
end
@@ -678,8 +678,8 @@ module ActiveRecord
private
- def exec_queries
- @records = eager_loading? ? find_with_associations.freeze : @klass.find_by_sql(arel, bound_attributes).freeze
+ def exec_queries(&block)
+ @records = eager_loading? ? find_with_associations.freeze : @klass.find_by_sql(arel, bound_attributes, &block).freeze
preload = preload_values
preload += includes_values unless eager_loading?