How to use alternate created_at / updated_at column


This article shows 2 ways for “How to use alternate created_at/updated_at column.


🤔 Configuration

Please add the following code to config/initializers/timestamp.rb.

module ActiveRecord
module Timestamp
private
def timestamp_attributes_for_update
[:modifyed_day]
end
def timestamp_attributes_for_create
[:created_day]
end
end
end

😀 Add callback to model

Please add callback before_update to a model class like this:

class Article < ApplicationRecord
before_update :set_modified_day

private

def set_modified_day
self.set_modified_day = Time.zone.now
end
end

🐠 Special Thanks

🖥 Recommended VPS Service

VULTR provides high performance cloud compute environment for you. Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour). In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!