:dependent => :delete_all not working

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

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

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

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

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

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

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

Gotcha. I thought I was missing someething. :slight_smile:

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

This works.

current_job.document.destroy
current_job.reload
current_job.build_document

Sweet.

Wes

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