# Implementing custom timestamps in AR

**URL:** <https://www.ruby-forum.com/t/implementing-custom-timestamps-in-ar/158695>\
**Category:** Rails\
**Created:** [January 26, 2009, 10:46pm UTC](https://www.ruby-forum.com/t/implementing-custom-timestamps-in-ar/158695 "2009-01-26T22:46:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [January 26, 2009, 10:46pm UTC](https://www.ruby-forum.com/t/implementing-custom-timestamps-in-ar/158695/1 "2009-01-26T22:46:03Z")

</div>

I have a requirement to record the last accessed date and time on  
several tables. I desire to do this in a local modification to AR using  
an initializer. I am looking at timestamp.rb to see how Rails handles  
the builtin timestamp attributes, but I am completely lost. Can some  
one explain to me what this code is doing?

module ActiveRecord  
module Timestamp  
def self.included(base) #:nodoc:  
base.alias\_method\_chain :create, :timestamps  
base.alias\_method\_chain :update, :timestamps

```
  base.class_inheritable_accessor :record_timestamps,
                                  :instance_writer => false
  base.record_timestamps = true
end

private
  def create_with_timestamps #:nodoc:
    if record_timestamps

```

…

I can figure out what the private method in timestamp.rb does, and it is  
pretty much what I want to do with my custom attributes. What I cannot  
figure out is how it is invoked. As far as I can determine, the only  
methods defined as :timestamps occur in the abstract connector class  
while the attribute record\_timestamps occurs, and is referenced, only  
within timestamp.rb itself.

My interpretation of this is that the base.class\_inheritable\_accessor  
creates a virtual attribute called record\_timestamps, prohibits creation  
of a setter, and sets the attribute value to true. But, I have no idea  
how or where the create\_with\_timestamps method is called. And I cannot  
fathom what the method\_chain is doing since the only timestamps method  
that I can find has to do with setting up columns in the schema via  
migrations. Finally, I do not see where the configuration value of  
config.record\_timestamps is used.
