我希望在我的Ruby on Rails应用程序中更新任何特定字段的时间戳。
我有一个"dprojects"
表,其中有一列用于"status"
和"status_date"
。
我怎样才能使状态更新时/当状态更新时,status_date
也会更改为最近更新的状态的日期?
如果状态列已更改,您可以利用 ActiveRecord 的脏属性跟踪来设置状态日期。使用before_save回调,然后根据需要设置状态日期:
class MyModel < ActiveRecord::Base
before_save :set_status_date
def set_status_date
self.status_date = Time.now if status_changed?
end
end