Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #1183 (closed defect: fixed)

Opened 3 years ago

Last modified 3 years ago

dependents are destroyed before client before_destroy hooks are called

Reported by: minam Assigned to: Jeremy Kemper <rails@bitsweat.net>
Priority: normal Milestone: 1.0
Component: ActiveRecord Version: 0.12.1
Severity: normal Keywords:
Cc:

Description

Given: two AR's, Post and Comment, where Post has_many Comments, and the :comments attribute is exclusively dependent on the parent post. Also given: the Post has a before_destroy callback. Result, the post's before_destroy callback is called _after_ the comments for the post are destroyed, making it impossible for the before_destroy callback to know what the comments were for the post being destroyed.

A workaround is to override the destroy method, but it might be nice to find a way to make before_destroy ALWAYS be called before any destructive actions on the object or its dependents.

Change History

04/23/05 20:54:27 changed by bitsweat

Using an after_destroy callback makes sense, but the record is frozen so you can't iterate over its associations.

Even then, a user's after_destroy callback attempting to manipulate an association would fail similarly.

04/26/05 22:01:52 changed by anonymous

  • milestone set to 1.0.

09/17/05 19:05:59 changed by nzkoz

  • keywords set to fd.

09/26/05 19:17:11 changed by bitsweat

Another workaround is to eschew the shortcut and put the callback in the correct order by hand:

class Post
  before_destroy { |post| do_stuff_with_post_comments }
  before_destroy { |post| post.comments.each { |comment| comment.destroy } }
end

or

class Post
  before_destroy do |post|
    post.comments.each do |comment|
      # ...
      comment.destroy
    end
  end
end

etc.

I think the fd keyword should be removed.

09/26/05 19:56:26 changed by nzkoz

  • keywords deleted.
  • milestone deleted.

Seconded

11/21/05 17:02:15 changed by anonymous

  • status changed from new to closed.
  • resolution set to invalid.

11/21/05 17:02:37 changed by anonymous

  • status changed from closed to reopened.
  • resolution deleted.

11/21/05 17:16:22 changed by bitsweat

  • owner changed from David to bitsweat.
  • status changed from reopened to new.
  • milestone set to 1.0.

References #2065.

02/13/06 04:08:08 changed by BobSilva

  • status changed from new to closed.
  • resolution set to fixed.

Changeset[2940]