我該如何做到這一點?如何在不觸摸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