mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Fix #3672 again (dependent: delete_all perf)
This commit is contained in:
@@ -18,7 +18,7 @@ module ActiveRecord::Associations::Builder
|
||||
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
|
||||
|
||||
@@ -44,12 +44,18 @@ module ActiveRecord::Associations::Builder
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
@@ -152,6 +152,13 @@ module ActiveRecord
|
||||
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.
|
||||
|
||||
@@ -32,6 +32,10 @@ module ActiveRecord
|
||||
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
|
||||
|
||||
@@ -89,12 +89,8 @@ module ActiveRecord
|
||||
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 @@ module ActiveRecord
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def foreign_key_present?
|
||||
owner.attribute_present?(reflection.association_primary_key)
|
||||
end
|
||||
|
||||
@@ -54,6 +54,10 @@ module ActiveRecord
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user