touch

touch(name = nil)
Instance Public methods

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, after_commit and after_rollback callbacks are executed. If an attribute name is passed, that attribute is updated along with updated_at/on attributes.

1
2
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.

1
2
3
4
5
6
7
8
9
10
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:

1
2
ball = Ball.new
ball.touch(:updated_at)   # => raises ActiveRecordError
doc_ruby_on_rails
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.