Class: ActiveRecord::Associations::HasManyAssociation

Inherits:
Object
  • Object
show all
Includes:
CompositePrimaryKeys::Predicates
Defined in:
lib/composite_primary_keys/associations/has_many_association.rb

Instance Method Summary collapse

Methods included from CompositePrimaryKeys::Predicates

#cpk_and_predicate, #cpk_id_predicate, #cpk_in_predicate, #cpk_join_predicate, #cpk_or_predicate

Instance Method Details

#delete_count(method, scope) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/composite_primary_keys/associations/has_many_association.rb', line 21

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

#delete_records(records, method) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/composite_primary_keys/associations/has_many_association.rb', line 6

def delete_records(records, method)
  if method == :destroy
    records.each(&:destroy!)
    update_counter(-records.length) unless reflection.inverse_updates_counter_cache?
    return
  # Zerista
  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)
  else
    scope = self.scope.where(reflection.klass.primary_key => records)
  end
  update_counter(-delete_count(method, scope))
end

#foreign_key_present?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
# File 'lib/composite_primary_keys/associations/has_many_association.rb', line 35

def foreign_key_present?
  if reflection.klass.primary_key
    # CPK
    # owner.attribute_present?(reflection.association_primary_key)
    Array(reflection.klass.primary_key).all? {|key| owner.attribute_present?(key)}
  else
    false
  end
end