Kleber S. Kleber S. - 1 month ago 5
Ruby Question

How to update a single attribute without touch updated_at attribute?

How can I achieve this?

tried to create 2 methods, called

def disable_timestamps
ActiveRecord::Base.record_timestamps = false
end

def enable_timestamps
ActiveRecord::Base.record_timestamps = true
end


and the update method itself:

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


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.

Answer
Comments