From 5aab0c053832ded70a3a4b58cb97f8f8bba796ba Mon Sep 17 00:00:00 2001 From: Brian Thomas Storti Date: Sat, 23 Nov 2013 09:24:52 -0200 Subject: 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`. --- activerecord/test/models/bulb.rb | 6 ++++++ activerecord/test/models/car.rb | 1 + 2 files changed, 7 insertions(+) (limited to 'activerecord/test/models') diff --git a/activerecord/test/models/bulb.rb b/activerecord/test/models/bulb.rb index 4361188e21..831a0d5387 100644 --- a/activerecord/test/models/bulb.rb +++ b/activerecord/test/models/bulb.rb @@ -43,3 +43,9 @@ class FunkyBulb < Bulb raise "before_destroy was called" end end + +class FailedBulb < Bulb + before_destroy do + false + end +end diff --git a/activerecord/test/models/car.rb b/activerecord/test/models/car.rb index 8f3b70a7c6..c4a15a79e2 100644 --- a/activerecord/test/models/car.rb +++ b/activerecord/test/models/car.rb @@ -2,6 +2,7 @@ class Car < ActiveRecord::Base has_many :bulbs has_many :all_bulbs, -> { unscope where: :name }, class_name: "Bulb" has_many :funky_bulbs, class_name: 'FunkyBulb', dependent: :destroy + has_many :failed_bulbs, class_name: 'FailedBulb', dependent: :destroy has_many :foo_bulbs, -> { where(:name => 'foo') }, :class_name => "Bulb" has_one :bulb -- cgit v1.2.3