Method: Dynamoid::Persistence#touch

Defined in:
lib/dynamoid/persistence.rb

#touch(*names, time: nil) ⇒ Dynamoid::Document

Update document timestamps.

Set updated_at attribute to current DateTime.

post.touch

Can update other fields in addition with the same timestamp if their names passed as arguments.

user.touch(:last_login_at, :viewed_at)

Some specific value can be used to save:

user.touch(time: 1.hour.ago)

No validation is performed and only after_touch callback is called.

The method must be used on a persisted object, otherwise Dynamoid::Errors::Error will be thrown.

Parameters:

  • names (*Symbol)

    a list of attribute names to update (optional)

  • time (Time) (defaults to: nil)

    datetime value that can be used instead of the current time (optional)

Returns:



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/dynamoid/persistence.rb', line 476

def touch(*names, time: nil)
  if new_record?
    raise Dynamoid::Errors::Error, 'cannot touch on a new or destroyed record object'
  end

  time_to_assign = time || DateTime.now

  self.updated_at = time_to_assign
  names.each do |name|
    attributes[name] = time_to_assign
  end

  attribute_names = names.map(&:to_sym) + [:updated_at]
  attributes_with_values = attributes.slice(*attribute_names)

  run_callbacks :touch do
    self.class.update_fields(hash_key, range_value, attributes_with_values)
    clear_attribute_changes(attribute_names.map(&:to_s))
  end

  self
end