From e247f3257927e008ed89944249ac38a8838f719f Mon Sep 17 00:00:00 2001 From: eileencodes Date: Wed, 26 Mar 2014 10:06:13 -0400 Subject: fix delete_all to remove records directly When delete_all is run on a CollectionProxy and has a dependency of delete_all the SQL that is produced has an IN statement. (DELETE FROM `associated_model` where `associated_model` .`parent_id` = 1 AND `associated_model`.`id` IN (1, 2, 3...)). This only happens if the association is not loaded (both loaded and non-loaded delete_all should behave the same. This is a huge problem when it comes to deleting many records because the query becomes very slow. Instead the SQL produced should be (DELETE FROM `assoicated_model` where `associated_model`.`parent_model_id`=1). I fixed this by making sure the check for loaded and destroy also makes sure that the dependent is not delete_all, so the conditional goes to the else and deletes the records directly without the IN statement. --- activerecord/lib/active_record/associations/collection_association.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 1f314e0677..80ae38b3fb 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -248,7 +248,7 @@ module ActiveRecord dependent = _options[:dependent] || options[:dependent] if records.first == :all - if loaded? || dependent == :destroy + if (loaded? || dependent == :destroy) && dependent != :delete_all delete_or_destroy(load_target, dependent) else delete_records(:all, dependent) -- cgit v1.2.3