module ActiveRecord::Persistence
def touch(*names, time: nil)
ball.touch(:updated_at) # => raises ActiveRecordError
ball = Ball.new
ActiveRecordError will be thrown. For example:
Note that +touch+ must be used on a persisted object, or else an
@brake.touch
# triggers @brake.car.touch and @brake.car.corporation.touch
end
belongs_to :corporation, touch: true
class Car < ActiveRecord::Base
end
belongs_to :car, touch: true
class Brake < ActiveRecord::Base
then +touch+ will invoke +touch+ method on associated object.
If used along with {belongs_to}[rdoc-ref:Associations::ClassMethods#belongs_to]
product.touch(:started_at, :ended_at) # updates started_at, ended_at and updated_at/on attributes
product.touch(:designed_at) # updates the designed_at attribute and updated_at/on
product.touch(time: Time.new(2015, 2, 16, 0, 0, 0)) # updates updated_at/on with specified time
product.touch # updates updated_at/on with current time
attributes. If no time argument is passed, the current time is used as default.
If attribute names are passed, they are updated along with updated_at/on
This method can be passed attribute names and an optional time argument.
+after_commit+ and +after_rollback+ callbacks are executed.
Please note that no validation is performed and only the +after_touch+,
or the time specified.
Saves the record with the updated_at/on attributes set to the current time
def touch(*names, time: nil) _raise_record_not_touched_error unless persisted? _raise_readonly_record_error if readonly? attribute_names = timestamp_attributes_for_update_in_model attribute_names |= names.map! do |name| name = name.to_s self.class.attribute_aliases[name] || name end unless names.empty? unless attribute_names.empty? affected_rows = _touch_row(attribute_names, time) @_trigger_update_callback = affected_rows == 1 else true end end