Forum: Ruby on Rails Implementing custom timestamps in AR

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-01-26 22:46
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.
This topic is locked and can not be replied to.