Sha256: ab77cd5f26bc844563a4095eb80502c4c7ae1a9dfde7bf0bfea95da847aee1b0

Contents?: true

Size: 1.18 KB

Versions: 17

Compression:

Stored size: 1.18 KB

Contents

module ActiveRecord
  module Associations
    class HasManyAssociation
      def delete_records(records, method)
        if method == :destroy
          records.each(&:destroy!)
          update_counter(-records.length) unless reflection.inverse_updates_counter_cache?
        # CPK
        elsif self.reflection.klass.composite?
          predicate = cpk_in_predicate(self.scope.table, self.reflection.klass.primary_keys, records.map(&:id))
          scope = self.scope.where(predicate)
          update_counter(-delete_count(method, scope))
        else
          scope = self.scope.where(reflection.klass.primary_key => records)
          update_counter(-delete_count(method, scope))
        end
      end

      def delete_count(method, scope)
        if method == :delete_all
          scope.delete_all
        else
          # CPK
          # scope.update_all(nullified_owner_attributes)
          conds = Array(reflection.foreign_key).inject(Hash.new) do |mem, key|
            mem[key] = nil
            mem
          end
          conds[reflection.type] = nil if reflection.type.present?
          scope.update_all(conds)
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
composite_primary_keys-13.0.1 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-13.0.0 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.10 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.9 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.8 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.6 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.5 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.4 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.3 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.2 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.1 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.0 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.0.rc5 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.0.rc4 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.0.rc3 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.0.rc2 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-12.0.0.rc1 lib/composite_primary_keys/associations/has_many_association.rb