Sha256: 89a1e22b1176a2b7570132f560a418f0c3cc935ea42a0416c15d353dbbb15b44

Contents?: true

Size: 909 Bytes

Versions: 1

Compression:

Stored size: 909 Bytes

Contents

module ActiveRecord
  module CounterCache
    module ClassMethods
      def update_counters(id, counters)
        touch = counters.delete(:touch)

        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

        if touch
          touch_updates = touch_updates(touch)
          updates << sanitize_sql_for_assignment(touch_updates) unless touch_updates.empty?
        end

        # CPK
        if primary_key.is_a?(Array)
          predicate = self.cpk_id_predicate(self.arel_table, self.primary_key, id)
          unscoped.where(predicate).update_all updates.join(", ")
        else
          unscoped.where(primary_key => id).update_all updates.join(", ")
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
composite_primary_keys-11.0.0.rc2 lib/composite_primary_keys/counter_cache.rb