I've heard of Rails' ActiveRecord/ActiveModel having something called "touch". However, I haven't found anything describing the purpose of this functionality.
What does it do, and what is it useful for?
In rails touch is used to update the updated_at field for persisted objects. But if we pass other attributes as arguments, it will update those fields too along with updated_at . Even it will update non-date-time fields if passed as arguments.
:touch If true, the associated object will be touched (the updated_at/on attributes set to now) when this record is either saved or destroyed. If you specify a symbol, that attribute will be updated with the current time in addition to the updated_at/on attribute. belongs_to :company, :touch => true.
ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.
Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.
As per the API Documentation, it is a method that only updates the specified timestamps of the model with the current time. So in order to update the updated_at
field:
product.touch
or to update the updated_at
and designed_at
fields:
product.touch(:designed_at)
Now, I have never used this method before, but I'd think it would be useful in a situation to "bump" a question (like on Stack Overflow) to the top of a search query without actually changing the contents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With