diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2016-08-31 08:54:38 -0400 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2016-08-31 09:26:25 -0400 |
commit | caa178c178468f7adcc5f4c597280e6362d9e175 (patch) | |
tree | 92e31877f28485d694d5e55bc315ef36b924ca37 /activerecord/test | |
parent | 0ca595ea6e3a9523b7902b05305437054de67e43 (diff) | |
download | rails-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/test')
-rw-r--r-- | activerecord/test/cases/associations/inverse_associations_test.rb | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb index 0b23cea420..6fe6ee6783 100644 --- a/activerecord/test/cases/associations/inverse_associations_test.rb +++ b/activerecord/test/cases/associations/inverse_associations_test.rb @@ -494,6 +494,33 @@ class InverseHasManyTests < ActiveRecord::TestCase assert !man.persisted? end + + def test_inverse_instance_should_be_set_before_find_callbacks_are_run + reset_callbacks(Interest, :find) do + Interest.after_find { raise unless association(:man).loaded? && man.present? } + + assert Man.first.interests.reload.any? + assert Man.includes(:interests).first.interests.any? + assert Man.joins(:interests).includes(:interests).first.interests.any? + end + end + + def test_inverse_instance_should_be_set_before_initialize_callbacks_are_run + reset_callbacks(Interest, :initialize) do + Interest.after_initialize { raise unless association(:man).loaded? && man.present? } + + assert Man.first.interests.reload.any? + assert Man.includes(:interests).first.interests.any? + assert Man.joins(:interests).includes(:interests).first.interests.any? + end + end + + def reset_callbacks(target, type) + old_callbacks = target.send(:get_callbacks, type).deep_dup + yield + ensure + target.send(:set_callbacks, type, old_callbacks) if old_callbacks + end end class InverseBelongsToTests < ActiveRecord::TestCase |