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.

1
2
3
4
5
6
7
8
9
10
11
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:

1
2
3
4
5
6
7
8
9
class Article < ApplicationRecord
before_update :set_modified_day
private
def set_modified_day
self.set_modified_day = Time.zone.now
end
end

Special Thanks