Come posso ottenere questo risultato?Come aggiornare un singolo attributo senza toccare l'attributo updated_at?
cercato di creare 2 metodi, chiamati
def disable_timestamps
ActiveRecord::Base.record_timestamps = false
end
def enable_timestamps
ActiveRecord::Base.record_timestamps = true
end
e il metodo di aggiornamento stesso:
def increment_pagehit
update_attribute(:pagehit, pagehit+1)
end
timestamp accendere e spegnere utilizzando callback come:
before_update :disable_timestamps, :only => :increment_pagehit
after_update :enable_timestamps, :only => :increment_pagehit
ma non è aggiornando qualsiasi cosa, anche l'attributo desiderato (pagehit).
Qualche consiglio? Non voglio dover creare un altro tavolo solo per contare le pagehits.
Provare a utilizzare 'update_attributes! (: Pagehit => pagehit + 1)' e vedere se si verificano errori. BTW, hai incollato 'def disable_timestamps' due volte per errore qui, o è lo stesso nel tuo codice? – Zabba
Possibile duplicato di [C'è un modo per evitare di aggiornare automaticamente i campi di data e ora di Rails?] (Http://stackoverflow.com/questions/861448/is-there-a-way-to-avoid-automatically-updating-rails-timestamp -fields) –