Permalink
Browse files

Perf fix

If we're deleting all records in an association, don't add a IN(..)
clause to the query.

Fixes #3672.
  • Loading branch information...
1 parent 2ef4947 commit fec85cf10d831ab90386a549ea558b0dd58a813f @jonleighton jonleighton committed Nov 19, 2011
Showing with 11 additions and 2 deletions.
  1. +5 −0 activerecord/CHANGELOG.md
  2. +6 −2 activerecord/lib/active_record/associations/has_many_association.rb
View
5 activerecord/CHANGELOG.md
@@ -67,6 +67,11 @@
## Rails 3.1.3 (unreleased) ##
+* Perf fix: If we're deleting all records in an association, don't add a IN(..) clause
+ to the query. *GH 3672*
+
+ *Jon Leighton*
+
* Fix bug with referencing other mysql databases in set_table_name. *GH 3690*
* Fix performance bug with mysql databases on a server with lots of other databses. *GH 3678*
View
8 activerecord/lib/active_record/associations/has_many_association.rb
@@ -89,8 +89,12 @@ def delete_records(records, method)
records.each { |r| r.destroy }
update_counter(-records.length) unless inverse_updates_counter_cache?
else
- keys = records.map { |r| r[reflection.association_primary_key] }
- scope = scoped.where(reflection.association_primary_key => keys)
+ scope = scoped
+
+ unless records == load_target
+ keys = records.map { |r| r[reflection.association_primary_key] }
+ scope = scoped.where(reflection.association_primary_key => keys)
+ end
if method == :delete_all
update_counter(-scope.delete_all)

0 comments on commit fec85cf

Please sign in to comment.