Destroyed object causing problems in it's last throes


#1

I have a Question object which has an after_destroy callback which
deletes
associated Answers.

During interactive testing using the console, I noticed that the
after_destroy macro gets fired even after a question object has been
previously destroyed. ie

q = Question.find :first
q.destroy => after_destroy is called
q.destroy => after_destroy is called again

Why won’t q stay dead?


#2

Am 24.11.2005 um 07:56 schrieb Hammed M.:

Why won’t q stay dead?

Hm, it is “dead”. Maybe you could test if the ActiveRecord object has
been frozen - as it will be if it has been destroyed (“if
obj.frozen? …”).

Additionally, you could use the “:dependent => true” option of the
“has_many” macro. This way answers would automatically deleted on
database updates. Generally, using these “declarative” solutions to
problems should be preferred: Let Rails do the thing if it already is
able to do so. Don’t Roll Your Own ™.

Regards

Manuel H.


#3

Additionally, you could use the “:dependent => true” option of the
“has_many” macro. This way answers would automatically deleted on
database updates.

Thanks Manuel. You’re right, I’ll add :dependent for the deletion.
However,
there’s other housekeeping tasks that I need to run after a question has
been deleted (update several different counters, update scores etc.)
which
still require the after_destroy callback.

This problem isn’t really an issue except for when I’m testing and am
able
to call destroy on a previously destroyed object. I was just curious if
this
behaviour should be considered a bug or if it is desired functionality
for
some reason.

Hammed