Forum: Ruby on Rails Improve(?) has_many :dependent => :destroy before destroy callback

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Michal P. (Guest)
on 2009-02-03 23:40
(Received via mailing list)
I tried to submit this via lighthouse, but after third POST failure
(mail also failed) I thought maybe someone here will be interested and
forward this further.

In my application I have dependent models and I wanted to use
before_destroy callback to "validate" if dependent item could be

class Squad < ActiveRecord::Base
 has_many :trolls, :dependent => :destroy

class Troll < ActiveRecord::Base
 belongs_to :squad
 before_destroy :destroy_cb

 def destroy_cb
   # I am indestructible!

Trying to destroy troll fails as expected, but when I destroy Squad,
it is disbanded but members are still alive, go berserk and trash my
database (foreign key points to invalid - removed - squad).
When I command "Squad.first.destroy" I expect failure because my
minions are indestructible - dependent-destroy should fail.

I propose following change: try to destroy all dependent items - as it
is done now - then: if any dependent destroy operation failed, return
failure and do not progress further.

(patch inline)
--- activerecord-2.2.2/lib/active_record/associations.rb.orig
2009-02-03 17:29:13.000000000 +0100
+++ activerecord-2.2.2/lib/active_record/associations.rb  2009-02-03
17:30:49.000000000 +0100
@@ -1452,7 +1452,7 @@
               when :destroy
                 method_name = "has_many_dependent_destroy_for_#
                 define_method(method_name) do
-                  send( { |o| o.destroy }
+                  send( { |o| o.destroy }.all?
                 before_destroy method_name
               when :delete_all
This topic is locked and can not be replied to.