# Before\_destroy not called

**URL:** <https://www.ruby-forum.com/t/before-destroy-not-called/112437>\
**Category:** Rails\
**Created:** [August 18, 2007, 11:34pm UTC](https://www.ruby-forum.com/t/before-destroy-not-called/112437 "2007-08-18T23:34:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dailer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dailer](https://www.ruby-forum.com/u/dailer)\
**Post date:** [August 18, 2007, 11:34pm UTC](https://www.ruby-forum.com/t/before-destroy-not-called/112437/1 "2007-08-18T23:34:10Z")

</div>

I have a situation where it seems rails should delete dependent  
records but does not:

class Order \< ActiveRecord::Base  
has\_many :line\_items, :dependent =\> :destroy  
end

class LineItem \< ActiveRecord::Base  
belongs\_to :order  
end

```
o=Order.new
li=LineItem.new
o.line_items<<li
o.save!
Order.delete(o.id)

```

This deletes the order but not the line item. I did a little debugging  
and see that rails adds the code (associations.rb):

```
      case reflection.options[:dependent]
        when :destroy, true
          module_eval "before_destroy '#{reflection.name}.each { |

```

o| o.destroy }’"

but if I add a before\_destroy to Order, it is never called. There was  
another post on this but I didn’t really see an explanation for this.  
More of a workaround.

Rails is great and all, but running into what seems to be very simple  
cases that don’t work and having to step thru very cryptic rails code  
isn’t much fun.

I guess I’ll add cascade/delete to the database tables…

---

<div class="post-metadata">

**Author:** ![dailer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dailer](https://www.ruby-forum.com/u/dailer)\
**Post date:** [August 18, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/before-destroy-not-called/112437/2 "2007-08-18T23:37:10Z")

</div>

delete skips any callbacks, use destroy if you want the callbacks to get  
invoked.

```
o=Order.new
li=LineItem.new
o.line_items<<li
o.save!
o.destroy

```

dailer wrote:

> ```
> when :destroy, true
> 
> ```
> 
> I guess I’ll add cascade/delete to the database tables…
> 
> >

–  
Sincerely,

William P.

[http://www.billpratt.net](http://www.billpratt.net)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![dailer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@dailer](https://www.ruby-forum.com/u/dailer)\
**Post date:** [August 19, 2007, 12:37am UTC](https://www.ruby-forum.com/t/before-destroy-not-called/112437/3 "2007-08-19T00:37:13Z")

</div>

ugh. that was too easy.
