Forum: Ruby on Rails Destroyed object causing problems in it's last throes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
hammed (Guest)
on 2005-11-24 08:57
(Received via mailing list)
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?
purestorm (Guest)
on 2005-11-24 10:58
(Received via mailing list)
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 (tm).

Regards

Manuel H.
hammed (Guest)
on 2005-11-24 17:50
(Received via mailing list)
>
> 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
This topic is locked and can not be replied to.