我该如何做到这一点?如何在不触摸updated_at属性的情况下更新单个属性?
试图建立2种方法,称为
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
和关闭使用回调,比如开时间戳:
before_update :disable_timestamps, :only => :increment_pagehit
after_update :enable_timestamps, :only => :increment_pagehit
,但它不是更新任何东西,甚至是所需的属性(pagehit)。
有什么建议吗?我不想为了统计页面数量而创建另一个表格。
尝试使用`update_attributes!(:pagehit => pagehit + 1)`并查看是否有任何错误。顺便说一句,你是否在这里粘贴了`def disable_timestamps`两次,或者你的代码是否一样? – Zabba 2011-02-03 15:02:01
[有没有办法避免自动更新Rails时间戳字段?](http://stackoverflow.com/questions/861448/is-there-a-way-to-avoid-automatically-updating-rails-timestamp场地) – 2016-05-12 18:46:30