我可以在不添加额外列或使用 updated_at 的情况下从状态更改中获取时间吗?
Can I get the time from the status change without adding additional column or using updated_at?
How to detect attribute changes from model?关于这个问题,请问能看到状态变化的时间吗?请指导和帮助。
您可以利用 rails Active Model Dirty 检查模型中的特定列是否已更改。
例如,假设一个 Car
class 包含您想要观察的列 status
。作为Railsauto-generates方法status_changed?
,您可以将此作为条件检查来执行您的自定义方法并读取时间。
class Car
before_update :my_custom_method, if: :status_changed?
def my_custom_method
# read the time
end
end
How to detect attribute changes from model?关于这个问题,请问能看到状态变化的时间吗?请指导和帮助。
您可以利用 rails Active Model Dirty 检查模型中的特定列是否已更改。
例如,假设一个 Car
class 包含您想要观察的列 status
。作为Railsauto-generates方法status_changed?
,您可以将此作为条件检查来执行您的自定义方法并读取时间。
class Car
before_update :my_custom_method, if: :status_changed?
def my_custom_method
# read the time
end
end