Method: Dynamoid::Persistence::ClassMethods#inc

Defined in:
lib/dynamoid/persistence.rb

#inc(hash_key_value, range_key_value = nil, counters) ⇒ Model class

Increase a numeric field by specified value.

User.inc('1', age: 2)

Can update several fields at once.

User.inc('1', age: 2, version: 1)

If range key is declared for a model it should be passed as well:

User.inc('1', 'Tylor', age: 2)

It’s an atomic operation it does not interfere with other write requests.

Uses efficient low-level UpdateItem operation and does only one HTTP request.

Doesn’t run validations and callbacks. Doesn’t update created_at and updated_at as well.

When ‘:touch` option is passed the timestamp columns are updating. If attribute names are passed, they are updated along with updated_at attribute:

User.inc('1', age: 2, touch: true)
User.inc('1', age: 2, touch: :viewed_at)
User.inc('1', age: 2, touch: [:viewed_at, :accessed_at])

Parameters:

  • hash_key_value (Scalar value)

    hash key

  • range_key_value (Scalar value) (defaults to: nil)

    range key (optional)

  • counters (Hash)

    value to increase by

Options Hash (counters):

  • :touch (true | Symbol | Array<Symbol>)

    to update update_at attribute and optionally the specified ones

Returns:

  • (Model class)

    self



446
447
448
449
450
# File 'lib/dynamoid/persistence.rb', line 446

def inc(hash_key_value, range_key_value = nil, counters)
  # It's similar to Rails' #update_counters.
  Inc.call(self, hash_key_value, range_key_value, counters)
  self
end