我想制作一个将 last_time_accessed 记录为属性的 Rails 模型。因此,每当我访问一个实例时,都会保存当前时间。有谁知道如何做到这一点?
最佳答案
您可以使用 after_find、after_initialize 或 after_touch 模块回调。决定什么最适合您的需求。但我认为它很丑陋并且要注意,因此您可以通过大量写入请求给您的数据库带来压力。
class Model < ActiveRecord::Base
after_initialize :update_last_time_accessed
private
def update_last_time_accessed
update_attribute(:last_time_accessed, Time.now)
end
end
关于ruby-on-rails - Rails,如何制作记录上次访问时间的数据库模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35430518/