aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
authorAgis- <corestudiosinc@gmail.com>2015-08-24 11:27:06 +0300
committerAgis- <corestudiosinc@gmail.com>2015-08-24 11:49:43 +0300
commit19b168e611fb8fb547981c4535130c29856efd3a (patch)
tree14ad6450b7da0940102d7a384b66fb55e4404935 /activerecord/test/cases
parentef8d09d932e36b0614905ea5bc3fb6af318b6ce2 (diff)
downloadrails-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/test/cases')
-rw-r--r--activerecord/test/cases/associations/has_one_associations_test.rb8
1 files changed, 8 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb
index 5a8afaf4d2..d46e7ad235 100644
--- a/activerecord/test/cases/associations/has_one_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_associations_test.rb
@@ -107,6 +107,14 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_nil Account.find(old_account_id).firm_id
end
+ def test_nullification_on_destroyed_association
+ developer = Developer.create!(name: "Someone")
+ ship = Ship.create!(name: "Planet Caravan", developer: developer)
+ ship.destroy
+ assert !ship.persisted?
+ assert !developer.persisted?
+ end
+
def test_natural_assignment_to_nil_after_destroy
firm = companies(:rails_core)
old_account_id = firm.account.id