instance method
touch
Ruby on Rails 4.0.13
Since v3.0.20Signature
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 only the after_touch callback is 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
Note that touch must be used on a persisted object, or else an ActiveRecordError will be thrown. For example:
ball = Ball.new ball.touch(:updated_at) # => raises ActiveRecordError
Parameters
-
nameopt = nil
Source
# File activerecord/lib/active_record/persistence.rb, line 432
def touch(name = nil)
raise ActiveRecordError, "can not touch on a new record object" unless persisted?
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|
column = column.to_s
changes[column] = write_attribute(column, current_time)
end
changes[self.class.locking_column] = increment_lock if locking_enabled?
@changed_attributes.except!(*changes.keys)
primary_key = self.class.primary_key
self.class.unscoped.where(primary_key => self[primary_key]).update_all(changes) == 1
else
true
end
end
Defined in activerecord/lib/active_record/persistence.rb line 432
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Persistence