Reputation: 57
I'm looking to make a timestamp update of when any particular fields are updated in my Ruby on Rails app.
I have a "dprojects"
table that has a column for "status"
and "status_date"
.
How can I make it so that if/when the status is updated, the status_date
also changes to be the date the status that was most recently updated to?
Upvotes: 0
Views: 1608
Reputation: 24337
You can take advantage of ActiveRecord's dirty attribute tracking to set the status date if the status column was changed. Use a before_save callback and then set the status date if needed:
class MyModel < ActiveRecord::Base
before_save :set_status_date
def set_status_date
self.status_date = Time.now if status_changed?
end
end
Upvotes: 2