diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-05-18 20:48:11 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-05-18 20:55:12 +0100 |
commit | b98d1e21635d8776de8893cc09bd86c71f6c78f0 (patch) | |
tree | e2b3a7fe3982e4ae4cfaa718a9165d4f3243d9b7 /activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb | |
parent | 2802ad0f72cd030efc3c4ec4f033389564609cec (diff) | |
download | rails-b98d1e21635d8776de8893cc09bd86c71f6c78f0.tar.gz rails-b98d1e21635d8776de8893cc09bd86c71f6c78f0.tar.bz2 rails-b98d1e21635d8776de8893cc09bd86c71f6c78f0.zip |
Perf: Don't load the association for #delete_all.
Bug #6289
Conflicts:
activerecord/test/cases/associations/has_many_associations_test.rb
Diffstat (limited to 'activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index a4cea99372..e8ae3e51b9 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -46,11 +46,17 @@ module ActiveRecord if sql = options[:delete_sql] records.each { |record| owner.connection.delete(interpolate(sql, record)) } else - relation = join_table - stmt = relation.where(relation[reflection.foreign_key].eq(owner.id). - and(relation[reflection.association_foreign_key].in(records.map { |x| x.id }.compact)) - ).compile_delete - owner.connection.delete stmt + relation = join_table + condition = relation[reflection.foreign_key].eq(owner.id) + + unless records == :all + condition = condition.and( + relation[reflection.association_foreign_key] + .in(records.map { |x| x.id }.compact) + ) + end + + owner.connection.delete(relation.where(condition).compile_delete) end end |