Forum: Ruby on Rails Extending AR to write audit columns

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-27 20:01
Rails 2.2.2
Ruby 1.8.5

I am attempting to extend AR with a generalized audit attribute setter.
I have written this code, which does not work for reasons unknown to me.
Perhaps someone here can explain why it does not.

# Put this in config/initializer as active_record_addins.rb
module ActiveRecord

  module HLLAuditStamps

    def self.included(base)
      super
      # alias_method_chain  :create,                :audit
      # is the same as:
      # alias_method        :create_without_audit,  :create
      # alias_method        :create,                :create_with_audit

      base.alias_method_chain  :create,                :audit
      base.alias_method_chain  :update,                :audit

    end

    private

    def create_with_audit
      write_audit_fields
      # Now we call our alias to the original method
      create_without_audit
    end

    def normal_time_now
      if base.class.default_timezone == :utc
        return DateTime.now.utc
      else
        return DateTime.now
      end
    end

    def update_with_audit
      write_audit_fields
      update_without_audit
    end

    def write_audit_fields
      write_attribute('accessed_at', normal_time_now) if \
          respond_to?('accessed_at')
      write_attribute('accessed_on', normal_time_now) if \
          respond_to?('accessed_on')
    end

  end

end

What I intend is that whenever the AR create or update method is called
then the attributes specified in write_audit_fields are set to the value
provided by normal_time_now.  They are not and I cannot tell why.

Any suggestions as to what I am doing wrong?
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-01-27 23:31
James Byrne wrote:
> Any suggestions as to what I am doing wrong?

Sorry this isn't a direct answer to your question, but I just wanted to
ask if you have looked at any of the existing auditing plugins to see if
they support your needs?

For example:
http://github.com/collectiveidea/acts_as_audited/tree/master
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-01-28 01:38
Robert Walker wrote:
> James Byrne wrote:
>> Any suggestions as to what I am doing wrong?
>
> Sorry this isn't a direct answer to your question, but I just wanted to
> ask if you have looked at any of the existing auditing plugins to see if
> they support your needs?
>
> For example:
> http://github.com/collectiveidea/acts_as_audited/tree/master

Despite the similarity of nomenclature, what I wish to do is different
than the service provided by that plugin, or acts_as_versioned, etc.
The example code given above is a minimalist representation of my
situation.

I have since read that the technique I am trying to employ has been
broken by a change to Rails (http://dev.rubyonrails.org/changeset/7749)
but I have been unable to discover a working alternative.
This topic is locked and can not be replied to.