Browse files

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
  • Loading branch information...
eileencodes committed Mar 26, 2014
1 parent a116afe commit e247f3257927e008ed89944249ac38a8838f719f
Showing with 1 addition and 1 deletion.
  1. +1 −1 activerecord/lib/active_record/associations/collection_association.rb
@@ -248,7 +248,7 @@ def delete(*records)
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)
delete_records(:all, dependent)

0 comments on commit e247f32

Please sign in to comment.