如何在不触及 updated_at 属性的情况下更新单个属性? [英] How to update a single attribute without touching the updated_at attribute?
本文介绍了如何在不触及 updated_at 属性的情况下更新单个属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎样才能做到这一点?
How can I achieve this?
尝试创建2个方法,称为
tried to create 2 methods, called
def disable_timestamps
ActiveRecord::Base.record_timestamps = false
end
def enable_timestamps
ActiveRecord::Base.record_timestamps = true
end
和更新方法本身:
def increment_pagehit
update_attribute(:pagehit, pagehit+1)
end
使用以下回调打开和关闭时间戳:
turn timestamps on and off using callbacks like:
before_update :disable_timestamps, :only => :increment_pagehit
after_update :enable_timestamps, :only => :increment_pagehit
但它没有更新任何内容,甚至是所需的属性(pagehit).
but it's not updating anything, even the desired attribute (pagehit).
有什么建议吗?我不想为了计算页面点击量而创建另一个表.
Any advice? I don't want to have to create another table just to count the pagehits.
推荐答案
或者更接近您的问题:
http://blog.bigbinary.com/2009/01/21/override-automatic-timestamp-in-activerecord-rails.html
这篇关于如何在不触及 updated_at 属性的情况下更新单个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文