Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix #3672 again (dependent: delete_all perf)

  • Loading branch information...
commit 889e8bee82ea4f75adb6de5badad512d2c615b7f 1 parent 8a37970
@jonleighton jonleighton authored
View
2  activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb
@@ -18,7 +18,7 @@ def define_destroy_hook
model.send(:include, Module.new {
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def destroy_associations
- association(#{name.to_sym.inspect}).delete_all
+ association(#{name.to_sym.inspect}).delete_all_on_destroy
super
end
RUBY
View
8 activerecord/lib/active_record/associations/builder/has_many.rb
@@ -46,10 +46,16 @@ class << o
def define_delete_all_dependency_method
name = self.name
mixin.redefine_method(dependency_method_name) do
+ association(name).delete_all_on_destroy
+ end
+ end
+
+ def define_nullify_dependency_method
+ name = self.name
+ mixin.redefine_method(dependency_method_name) do
send(name).delete_all
end
end
- alias :define_nullify_dependency_method :define_delete_all_dependency_method
def define_restrict_dependency_method
name = self.name
View
7 activerecord/lib/active_record/associations/collection_association.rb
@@ -152,6 +152,13 @@ def delete_all
end
end
+ # Called when the association is declared as :dependent => :delete_all. This is
+ # an optimised version which avoids loading the records into memory. Not really
+ # for public consumption.
+ def delete_all_on_destroy
+ scoped.delete_all
+ end
+
# Destroy all the records from this association.
#
# See destroy for more info.
View
4 activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -32,6 +32,10 @@ def insert_record(record, validate = true, raise = false)
record
end
+ # ActiveRecord::Relation#delete_all needs to support joins before we can use a
+ # SQL-only implementation.
+ alias delete_all_on_destroy delete_all
+
private
def count_records
View
10 activerecord/lib/active_record/associations/has_many_association.rb
@@ -89,12 +89,8 @@ def delete_records(records, method)
records.each { |r| r.destroy }
update_counter(-records.length) unless inverse_updates_counter_cache?
else
- scope = scoped
-
- unless records == load_target
- keys = records.map { |r| r[reflection.association_primary_key] }
- scope = scoped.where(reflection.association_primary_key => keys)
- end
+ keys = records.map { |r| r[reflection.association_primary_key] }
+ scope = scoped.where(reflection.association_primary_key => keys)
if method == :delete_all
update_counter(-scope.delete_all)
@@ -103,7 +99,7 @@ def delete_records(records, method)
end
end
end
-
+
def foreign_key_present?
owner.attribute_present?(reflection.association_primary_key)
end
View
4 activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -54,6 +54,10 @@ def insert_record(record, validate = true, raise = false)
record
end
+ # ActiveRecord::Relation#delete_all needs to support joins before we can use a
+ # SQL-only implementation.
+ alias delete_all_on_destroy delete_all
+
private
def through_association
Please sign in to comment.
Something went wrong with that request. Please try again.