Module: ActiveRecord::CounterCache

Defined in:
lib/composite_primary_keys/counter_cache.rb

Instance Method Summary collapse

Instance Method Details

#decrement_counter(counter_name, id) ⇒ Object



18
19
20
# File 'lib/composite_primary_keys/counter_cache.rb', line 18

def decrement_counter(counter_name, id)
  update_counters(id, counter_name => -1)
end

#update_counters(id, counters) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/composite_primary_keys/counter_cache.rb', line 3

def update_counters(id, counters)
  updates = counters.map do |counter_name, value|
    operator = value < 0 ? '-' : '+'
    quoted_column = connection.quote_column_name(counter_name)
    "#{quoted_column} = COALESCE(#{quoted_column}, 0) #{operator} #{value.abs}"
  end

  IdentityMap.remove_by_id(symbolized_base_class, id) if IdentityMap.enabled?

  # CPK
  # update_all(updates.join(', '), primary_key => id )
  primary_key_predicate = relation.cpk_id_predicate(self.arel_table, Array(self.primary_key), Array(id))
  update_all(updates.join(', '), primary_key_predicate)
end