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? _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Manuel Holtgrewe
2005-Nov-24 08:57 UTC
Re: Destroyed object causing problems in it''s last throes
Am 24.11.2005 um 07:56 schrieb Hammed Malik:> 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?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 Holtgrewe
Hammed Malik
2005-Nov-24 15:46 UTC
Re: Destroyed object causing problems in it''s last throes
> > 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 On 24/11/05, Manuel Holtgrewe <purestorm-nlpEiS6K5uusTnJN9+BGXg@public.gmane.org> wrote:> > > Am 24.11.2005 um 07:56 schrieb Hammed Malik: > > > 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? > > 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 Holtgrewe > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails