Sha256: 794bd8aa72b8d432019c40f5a6144dfe4ff16f94c0229c8c0f30a672e20bc61a

Contents?: true

Size: 1.11 KB

Versions: 12

Compression:

Stored size: 1.11 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(reflection.foreign_key => nil)
          conds = Array(reflection.foreign_key).inject(Hash.new) do |mem, key|
            mem[key] = nil
            mem
          end
          scope.update_all(conds)
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
composite_primary_keys-11.3.1 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-11.2.0 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-11.1.0 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-11.0.3 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-11.0.2 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-11.0.1 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-11.0.0 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-11.0.0.rc1 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-11.0.0.beta4 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-11.0.0.beta3 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-11.0.0.beta2 lib/composite_primary_keys/associations/has_many_association.rb
composite_primary_keys-11.0.0.beta1 lib/composite_primary_keys/associations/has_many_association.rb