diff options
author | Seb Jacobs <me@sebjacobs.com> | 2015-01-15 23:03:23 +0000 |
---|---|---|
committer | Seb Jacobs <me@sebjacobs.com> | 2015-01-16 11:41:43 +0000 |
commit | d5bf649a535948e70692e521ce3070595334e71b (patch) | |
tree | d1f56e9ff058eceba35785bbeb1d88e54e15e910 /activerecord/lib/active_record | |
parent | 090c5211ced7b728df6176d5c9fc7437c107beaf (diff) | |
download | rails-d5bf649a535948e70692e521ce3070595334e71b.tar.gz rails-d5bf649a535948e70692e521ce3070595334e71b.tar.bz2 rails-d5bf649a535948e70692e521ce3070595334e71b.zip |
Add support for bidirectional destroy dependencies
Prior to this commit if you defined a bidirectional relationship
between two models with destroy dependencies on both sides, a call to
`destroy` would result in an infinite callback loop.
Take the following relationship.
class Content < ActiveRecord::Base
has_one :content_position, dependent: :destroy
end
class ContentPosition < ActiveRecord::Base
belongs_to :content, dependent: :destroy
end
Calling `Content#destroy` or `ContentPosition#destroy` would result in
an infinite callback loop.
This commit changes the behaviour of `ActiveRecord::Callbacks#destroy`
so that it guards against subsequent callbacks.
Thanks to @zetter for demonstrating the issue with failing tests[1].
[1] rails#13609
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/callbacks.rb | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb index f44e5af5de..07769e8952 100644 --- a/activerecord/lib/active_record/callbacks.rb +++ b/activerecord/lib/active_record/callbacks.rb @@ -289,7 +289,14 @@ module ActiveRecord end def destroy #:nodoc: - _run_destroy_callbacks { super } + begin + @_destroy_callback_already_called ||= false + return if @_destroy_callback_already_called + @_destroy_callback_already_called = true + _run_destroy_callbacks { super } + ensure + @_destroy_callback_already_called = false + end end def touch(*) #:nodoc: |