Class: ActiveRecord::Associations::HasManyAssociation

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

Instance Method Summary collapse

Instance Method Details

#construct_sqlObject



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

def construct_sql
  case
    when @reflection.options[:finder_sql]
      @finder_sql = interpolate_and_sanitize_sql(@reflection.options[:finder_sql])

    when @reflection.options[:as]
      @finder_sql =
        "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_id = #{owner_quoted_id} AND " +
        "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}"
      @finder_sql << " AND (#{conditions})" if conditions

    else
      # CPK
      # @finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{owner_quoted_id}"
      @finder_sql = full_columns_equals(@reflection.table_name, @reflection.cpk_primary_key, owner_quoted_id)
      @finder_sql << " AND (#{conditions})" if conditions
  end

  construct_counter_sql
end

#delete_records(records) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/composite_primary_keys/associations/has_many_association.rb', line 25

def delete_records(records)
  case @reflection.options[:dependent]
    when :destroy
      records.each { |r| r.destroy }
    when :delete_all
      @reflection.klass.delete(records.map { |record| record.id })
    else
      relation = Arel::Table.new(@reflection.table_name)
      # CPK
      #relation.where(relation[@reflection.primary_key_name].eq(@owner.id).
      #    and(relation[@reflection.klass.primary_key].in(records.map { |r| r.id }))
      #).update(relation[@reflection.primary_key_name] => nil)

      id_predicate = nil
      owner_key_values = @reflection.cpk_primary_key.zip([@owner.id].flatten)
      owner_key_values.each do |key, value|
        eq = relation[key].eq(value)
        id_predicate = id_predicate ? id_predicate.and(eq) : eq
      end

      record_predicates = nil
      records.each do |record|
        keys = [@reflection.klass.primary_key].flatten
        values = [record.id].flatten

        record_predicate = nil
        keys.zip(values).each do |key, value|
          eq = relation[key].eq(value)
          record_predicate = record_predicate ? record_predicate.and(eq) : eq
        end
        record_predicates = record_predicates ? record_predicates.or(record_predicate) : record_predicate
      end

      relation = relation.where(id_predicate.and(record_predicates))

      nullify_relation = Arel::Table.new(@reflection.table_name)
      nullify = @reflection.cpk_primary_key.inject(Hash.new) do |hash, key|
        hash[nullify_relation[key]] = nil
        hash
      end

      relation.update(nullify)

      @owner.class.update_counters(@owner.id, cached_counter_attribute_name => -records.size) if has_cached_counter?
  end
end