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/lib/active_record/associations/has_many_association.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 0a23109b9b..72e0891702 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -108,7 +108,7 @@ module ActiveRecord # Deletes the records according to the :dependent option. def delete_records(records, method) if method == :destroy - records.each { |r| r.destroy } + records.each(&:destroy!) update_counter(-records.length) unless inverse_updates_counter_cache? else if records == :all -- cgit v1.2.3