diff options
author | Brian Thomas Storti <btstorti@gmail.com> | 2013-11-23 09:24:52 -0200 |
---|---|---|
committer | Brian Thomas Storti <btstorti@gmail.com> | 2013-11-25 19:30:07 -0200 |
commit | 5aab0c053832ded70a3a4b58cb97f8f8bba796ba (patch) | |
tree | 6f10893ea925ca124b7c57c6fb0cdb0073759f03 /activerecord/test/cases/associations | |
parent | 19dd2166103cc1d39d7346714c46f32191958981 (diff) | |
download | rails-5aab0c053832ded70a3a4b58cb97f8f8bba796ba.tar.gz rails-5aab0c053832ded70a3a4b58cb97f8f8bba796ba.tar.bz2 rails-5aab0c053832ded70a3a4b58cb97f8f8bba796ba.zip |
Raise `RecordNotDestroyed` when children can't be replaced
Fixes #12812
Raise `ActiveRecord::RecordNotDestroyed` when a child marked with
`dependent: destroy` can't be destroyed.
The following code:
```ruby
class Post < ActiveRecord::Base
has_many :comments, dependent: :destroy
end
class Comment < ActiveRecord::Base
before_destroy do
return false
end
end
post = Post.create!(comments: [Comment.create!])
post.comments = [Comment.create!]
````
would result in a `post` with two `comments`.
With this commit, the same code would raise a `RecordNotDestroyed`
exception, keeping the `post` with the same `comment`.
Diffstat (limited to 'activerecord/test/cases/associations')
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index a025d49fa3..45bc974025 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1770,4 +1770,15 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal [bulb1], car.bulbs assert_equal [bulb1, bulb2], car.all_bulbs.sort_by(&:id) end + + test "raises RecordNotDestroyed when replaced child can't be destroyed" do + car = Car.create! + original_child = FailedBulb.create!(car: car) + + assert_raise(ActiveRecord::RecordNotDestroyed) do + car.failed_bulbs = [FailedBulb.create!] + end + + assert_equal [original_child], car.reload.failed_bulbs + end end |