instance method touch

Ruby on Rails 3.0.20

Since v3.0.20

Available in: v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

touch(name = nil)

Saves the record with the updated_at/on attributes set to the current time. Please note that no validation is performed and no callbacks are executed. If an attribute name is passed, that attribute is updated along with updated_at/on attributes.

product.touch               # updates updated_at/on
product.touch(:designed_at) # updates the designed_at attribute and updated_at/on

If used along with belongs_to then touch will invoke touch method on associated object.

class Brake < ActiveRecord::Base
  belongs_to :car, :touch => true
end

class Car < ActiveRecord::Base
  belongs_to :corporation, :touch => true
end

# triggers @brake.car.touch and @brake.car.corporation.touch
@brake.touch

Parameters

name opt = nil
Source
# File activerecord/lib/active_record/persistence.rb, line 232
    def touch(name = nil)
      attributes = timestamp_attributes_for_update_in_model
      attributes << name if name
      unless attributes.empty?
        current_time = current_time_from_proper_timezone
        changes = {}

        attributes.each do |column|
          changes[column.to_s] = write_attribute(column.to_s, current_time)
        end

        @changed_attributes.except!(*changes.keys)
        primary_key = self.class.primary_key
        self.class.unscoped.update_all(changes, { primary_key => self[primary_key] }) == 1
      end
    end

Defined in activerecord/lib/active_record/persistence.rb line 232 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::Persistence

Type at least 2 characters to search.

↑↓ navigate · open · esc close