method
delete_records
v4.1.8 -
Show latest stable
- Class:
ActiveRecord::Associations::HasManyThroughAssociation
delete_records(records, method)private
No documentation available.
# File activerecord/lib/active_record/associations/has_many_through_association.rb, line 140
def delete_records(records, method)
ensure_not_nested
# This is unoptimised; it will load all the target records
# even when we just want to delete everything.
records = load_target if records == :all
scope = through_association.scope
scope.where! construct_join_attributes(*records)
case method
when :destroy
if scope.klass.primary_key
count = scope.destroy_all.length
else
scope.to_a.each do |record|
record.run_callbacks :destroy
end
arel = scope.arel
stmt = Arel::DeleteManager.new arel.engine
stmt.from scope.klass.arel_table
stmt.wheres = arel.constraints
count = scope.klass.connection.delete(stmt, 'SQL', scope.bind_values)
end
when :nullify
count = scope.update_all(source_reflection.foreign_key => nil)
else
count = scope.delete_all
end
delete_through_records(records)
if source_reflection.options[:counter_cache] && method != :destroy
counter = source_reflection.counter_cache_column
klass.decrement_counter counter, records.map(&:id)
end
if through_reflection.macro == :has_many && update_through_counter?(method)
update_counter(-count, through_reflection)
end
update_counter(-count)
end