Touch ActiveRecord::Associations
ActiveRecord::Associations#belongs_to
has an option of touch
.
If passed true
, the associated object will have its updated_at
/updated_on
attributes set to current time.
You can also pass a symbol, in that case that attribute will be updated with the current time in addition to the updated_at
/updated_on
class Comment < ApplicationRecord
# When a comment is saved/destroyed, its post will
# update the updated_at/updated_on to the current time
belongs_to :post, touch: true
# OR
# When a comment is saved/destroyed, its post will update both
# the updated_at/updated_on as well as comment_last_updated_at
belongs_to :post, touch: :comment_last_updated_at
end
TweetNote: No validations are performed when touching.