# :dependent =\> :delete\_all not working

**URL:** <https://www.ruby-forum.com/t/dependent-delete-all-not-working/92166>\
**Category:** Rails\
**Created:** [April 16, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/dependent-delete-all-not-working/92166 "2007-04-16T20:10:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 16, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/dependent-delete-all-not-working/92166/1 "2007-04-16T20:10:39Z")

</div>

All,

Rails 1.1.6  
Win XP  
MS SQL Server (ODBC mode)

I have the following class:

class Document \< ActiveRecord::Base  
has\_many :images, :dependent =\> :delete\_all  
has\_many :stylesheets, :dependent =\> :delete\_all

In one of my controllers, I do:

Document.destroy(valid\_doc\_id)

and _no deletes_ are performed against my database, which is clearly  
wrong.

If, however, I use script/console to do the same exact destroy call with  
the same id, the cascaded delete works just fine.

I can hand code the cascaded delete, of course, but would like for  
:dependent =\> :delete\_all to work as advertised.

I know that it’s a long shot, but has anyone seen anything like this?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 16, 2007, 8:23pm UTC](https://www.ruby-forum.com/t/dependent-delete-all-not-working/92166/2 "2007-04-16T20:23:21Z")

</div>

If I replace the call to the AR Base class method destroy with a call to  
the instance method destroy on that particular object, then the deletes  
appear to work correctly.

That’s strange - any ideas?

WG

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 17, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/dependent-delete-all-not-working/92166/3 "2007-04-17T18:57:48Z")

</div>

This seems like a reasonable idea. However, the fact that this object  
is frozen when you do a “destroy” on it keeps me from building a new  
one.

So I have current\_job.document, and I call current\_job.document.destroy  
and then I can’t call current\_job.build\_document (I get a “can’t modify  
frozen hash” error).

Frankly, I don’t understand why this happens - I’m not trying to modify  
the original document object, I’m trying to replace it with a whole  
different one.

In general, it appears that once you have an association, you can’t  
destroy it and then expect to be able to replace it with another object.  
What’s up with that?

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 17, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/dependent-delete-all-not-working/92166/4 "2007-04-17T20:15:07Z")

</div>

I must be missing something because it doesn’t seem reasonable to me  
that  
:dependent =\> :destroy wouldn’t be allowed to destroy the associated  
object.  
I must be missing something.

RSL

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 17, 2007, 10:30pm UTC](https://www.ruby-forum.com/t/dependent-delete-all-not-working/92166/5 "2007-04-17T22:30:58Z")

</div>

The destroy is allowed to happen but then you can’t replace the object

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 18, 2007, 1:12pm UTC](https://www.ruby-forum.com/t/dependent-delete-all-not-working/92166/6 "2007-04-18T13:12:35Z")

</div>

I feel really stupid right now but obviously I’ve missed something. I  
don’t  
see that you [or I] are trying to replace any objects but destroy them.  
Sorry for being so thick about this but I’m honestly confused. I  
presumed  
that the problem was some kind of reference somewhere that I did wrong  
and  
because it couldn’t clear, the associated object never deletes.

RSL

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 18, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/dependent-delete-all-not-working/92166/7 "2007-04-18T16:28:09Z")

</div>

You and I have the same problem vis a vis deleting and I am also calling  
.clear explicitly on the child objects. I too do not know why this is  
necessary.

My _additional_ issue is that once I do the destroy on the child object,  
I cannot then create a new child object because the reference is frozen.  
So I’m complaining about that :).

Buy that?

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 18, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/dependent-delete-all-not-working/92166/8 "2007-04-18T17:02:54Z")

</div>

Gotcha. I _thought_ I was missing someething. 🙂

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 19, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/dependent-delete-all-not-working/92166/9 "2007-04-19T23:17:46Z")

</div>

Someone suggested that I reload the current\_job model variable using

current\_job.reload

and then perhaps I can do

current\_job.build\_document(…)

Anyone know if this is correct?

WG

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 24, 2007, 10:03pm UTC](https://www.ruby-forum.com/t/dependent-delete-all-not-working/92166/10 "2007-04-24T22:03:57Z")

</div>

This works.

current\_job.document.destroy  
current\_job.reload  
current\_job.build\_document

Sweet.

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [April 17, 2007, 3:41pm UTC](https://www.ruby-forum.com/t/dependent-delete-all-not-working/92166/11 "2007-04-17T15:41:25Z")

</div>

I’m having similar problems with :dependent =\> :destroy not destroying  
or  
deleting the dependent records. I’m having to just call  
association.clear in  
the after\_destroy. Oddly, using association.clear in the before\_destroy  
isn’t working for me either. It feels like there’s something going on  
preventing the association#destroy to fail but I don’t have time to  
spend  
trying to figure it out this time. You’re not alone on this though.

RSL
