Skip to content

Commit fec85cf

Browse files
committed
Perf fix
If we're deleting all records in an association, don't add a IN(..) clause to the query. Fixes rails#3672.
1 parent 2ef4947 commit fec85cf

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

activerecord/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767

6868
## Rails 3.1.3 (unreleased) ##
6969

70+
* Perf fix: If we're deleting all records in an association, don't add a IN(..) clause
71+
to the query. *GH 3672*
72+
73+
*Jon Leighton*
74+
7075
* Fix bug with referencing other mysql databases in set_table_name. *GH 3690*
7176

7277
* Fix performance bug with mysql databases on a server with lots of other databses. *GH 3678*

activerecord/lib/active_record/associations/has_many_association.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,12 @@ def delete_records(records, method)
8989
records.each { |r| r.destroy }
9090
update_counter(-records.length) unless inverse_updates_counter_cache?
9191
else
92-
keys = records.map { |r| r[reflection.association_primary_key] }
93-
scope = scoped.where(reflection.association_primary_key => keys)
92+
scope = scoped
93+
94+
unless records == load_target
95+
keys = records.map { |r| r[reflection.association_primary_key] }
96+
scope = scoped.where(reflection.association_primary_key => keys)
97+
end
9498

9599
if method == :delete_all
96100
update_counter(-scope.delete_all)

0 commit comments

Comments
 (0)