delete_all gives special treatment to dependent option :destroy for associations #9567

Closed
neerajdotname opened this Issue Mar 5, 2013 · 1 comment

2 participants

@neerajdotname
Ruby on Rails member

This issue is continuation of discussion started on #7832 .

class Post < ActiveRecord::Base
  has_many :comments, dependent: :destroy
end

class Comment < ActiveRecord::Base
  belongs_to :post

  before_destroy :before_destroy_callback

  def before_destroy_callback
    raise "before_destroy_callback"
  end
end

1.9.3-p125 :033 > Post.first.comments.delete_all
Post Load (0.1ms)  SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT 1
Comment Load (0.2ms)  SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = ?  [["post_id", 1]]
(0.1ms)  begin transaction
(0.0ms)  rollback transaction
RuntimeError: before_destroy_callback
@neerajdotname
Ruby on Rails member

If the dependent option is :destroy then delete_all on associations works exactly like destroy_all .

cc @rafaelfranca @carlosantoniodasilva @fxn

@neerajdotname neerajdotname pushed a commit that referenced this issue Mar 11, 2013
Neeraj Singh Show warning message if delete_all is firing callbacks
`post.comments.delete_all` will fire callbacks if :dependent option is
:destroy . It will be fixed in Rails 4.1 . In the meantime display
a warning . Look at #9567 for details .
d71eaf3
@neerajdotname neerajdotname pushed a commit that referenced this issue Mar 12, 2013
Neeraj Singh dependent: :destroy should call destroy_all
Commit #9668 shows warning
when `delete_all` is invoked with `:dependent` option
`:destroy`.

Unfortunately invoking `Post.destroy_all` invokes
`post.comments.delete_all` as part of `has_many` callbacks.

This commit ensures that instead `post.comments.destroy_all` is
invoked and in the process no warning is generated.

See issue #9567 for details .
c3829d3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment