Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 .
  • Loading branch information...
commit d71eaf3e3fb9e9a27a40805b0b724667f3b7a16a 1 parent 464cd9d
Neeraj Singh neerajdotname authored
9 activerecord/lib/active_record/associations/collection_association.rb
View
@@ -204,6 +204,15 @@ def delete(*records)
dependent = options[:dependent]
if records.first == :all
+
+ if dependent && dependent == :destroy
+ message = 'In Rails 4.1 delete_all on associations would not fire callbacks. ' \
+ 'It means if the :dependent option is :destroy then the associated ' \
+ 'records would be deleted without loading and invoking callbacks.'
+
+ ActiveRecord::Base.logger ? ActiveRecord::Base.logger.warn(message) : $stderr.puts(message)
+ end
+
if loaded? || dependent == :destroy
delete_or_destroy(load_target, dependent)
else

3 comments on commit d71eaf3

Yasuo Honda

Is this an expected behavior to show this message when destroy_all executed? Here is the code of Oracle enhanced adapter rspec test.

  describe "on multiple tables" do
    before(:all) do
      @conn = ActiveRecord::Base.connection
      create_tables
      class ::Post < ActiveRecord::Base
        has_many :comments, dependent: :destroy
        has_context_index
      end
      class ::Comment < ActiveRecord::Base
        belongs_to :post, counter_cache: true
      end
    end

    after(:all) do
      drop_tables
      Object.send(:remove_const, "Comment")
      Object.send(:remove_const, "Post")
      ActiveRecord::Base.clear_cache! if ActiveRecord::Base.respond_to?(:"clear_cache!")
    end

    after(:each) do
      Post.destroy_all
    end

Here Post.destroy_all called, this message appears.

Neeraj Singh
Collaborator

@yahonda You are right. This message is indeed appearing for Post.destroy_all . I checked that this message does not appear for post.comments.destroy_all but did not bother to check for Post.destroy_all. Looking into it.

Yasuo Honda

Thanks.

Please sign in to comment.
Something went wrong with that request. Please try again.