1 # Monkey patch composite_primary_keys pending the resolution of:
2 # https://github.com/composite-primary-keys/composite_primary_keys/pull/170
5 class HasManyAssociation
6 def delete_records(records, method)
8 records.each { |r| r.destroy }
9 update_counter(-records.length) unless inverse_updates_counter_cache?
15 # keys = records.map { |r| r[reflection.association_primary_key] }
16 # scope = scope.where(reflection.association_primary_key => keys)
17 table = Arel::Table.new(reflection.table_name)
18 and_conditions = records.map do |record|
19 eq_conditions = Array(reflection.association_primary_key).map do |name|
20 table[name].eq(record[name])
22 Arel::Nodes::And.new(eq_conditions)
25 condition = and_conditions.shift
26 and_conditions.each do |and_condition|
27 condition = condition.or(and_condition)
30 scope = self.scope.where(condition)
33 if method == :delete_all
34 update_counter(-scope.delete_all)
37 # update_counter(-scope.update_all(reflection.foreign_key => nil))
38 updates = Array(reflection.foreign_key).inject(Hash.new) do |hash, name|
42 update_counter(-scope.update_all(updates))