class ActiveRecord::Associations::HasManyThroughAssociation

def delete_records(records, method)

def delete_records(records, method)
  ensure_not_nested
  scope = through_association.scope
  scope.where! construct_join_attributes(*records)
  scope = scope.where(through_scope_attributes)
  case method
  when :destroy
    if scope.model.primary_key
      count = scope.destroy_all.count(&:destroyed?)
    else
      scope.each(&:_run_destroy_callbacks)
      count = scope.delete_all
    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.collection? && update_through_counter?(method)
    update_counter(-count, through_reflection)
  else
    update_counter(-count)
  end
  count
end