diff options
author | Agis- <corestudiosinc@gmail.com> | 2015-08-24 11:27:06 +0300 |
---|---|---|
committer | Agis- <corestudiosinc@gmail.com> | 2015-08-24 11:49:43 +0300 |
commit | 19b168e611fb8fb547981c4535130c29856efd3a (patch) | |
tree | 14ad6450b7da0940102d7a384b66fb55e4404935 /activerecord/lib/active_record | |
parent | ef8d09d932e36b0614905ea5bc3fb6af318b6ce2 (diff) | |
download | rails-19b168e611fb8fb547981c4535130c29856efd3a.tar.gz rails-19b168e611fb8fb547981c4535130c29856efd3a.tar.bz2 rails-19b168e611fb8fb547981c4535130c29856efd3a.zip |
Only nullify persisted has_one target associations
Since after 87d1aba3c `dependent: :destroy` callbacks on has_one
assocations run *after* destroy, it is possible that a nullification is
attempted on an already destroyed target:
class Car < ActiveRecord::Base
has_one :engine, dependent: :nullify
end
class Engine < ActiveRecord::Base
belongs_to :car, dependent: :destroy
end
> car = Car.create!
> engine = Engine.create!(car: car)
> engine.destroy! # => ActiveRecord::ActiveRecordError: cannot update a
> destroyed record
In the above case, `engine.destroy!` deletes `engine` and *then* triggers the
deletion of `car`, which in turn triggers a nullification of `engine.car_id`.
However, `engine` is already destroyed at that point.
Fixes #21223.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/has_one_association.rb | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 5a92bc5e8a..1829453d73 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -65,7 +65,7 @@ module ActiveRecord when :destroy target.destroy when :nullify - target.update_columns(reflection.foreign_key => nil) + target.update_columns(reflection.foreign_key => nil) if target.persisted? end end end |