aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2014-03-26 10:06:13 -0400
committereileencodes <eileencodes@gmail.com>2014-03-31 19:28:44 -0400
commite247f3257927e008ed89944249ac38a8838f719f (patch)
tree56bd8a9a7d273a648c80719773fe0f4346310042
parenta116afe5365dfaa3e5f98749f98090872c7aaecc (diff)
downloadrails-e247f3257927e008ed89944249ac38a8838f719f.tar.gz
rails-e247f3257927e008ed89944249ac38a8838f719f.tar.bz2
rails-e247f3257927e008ed89944249ac38a8838f719f.zip
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.
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb2
1 files changed, 1 insertions, 1 deletions
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)