Forum: Ruby on Rails extending activerecord base with after_initialize

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.
webguy7890 (Guest)
on 2007-06-16 23:07
(Received via mailing list)
hi,

i am having problems getting after_initialize for activerecord
subclasses.  basically, i am trying to use the guid plugin by andy
singleton.  i am having problems extending the class to the plugins i
installed, so i figured i will just apply this to all the models in my
project.  i trled to add the following to my lib/ folder.  my goal is
to extended the activerecord class so that it uses guid everywhere.
but the after_initialize doesn't seem to be kicking in.  i think i m
missing a step in the self.included function, but not sure exactly
what.  i tried to use the AR:validation and other classes as
reference, but i couldn't get it to work.  any insight into what i am
doing wrong is greatly appreciated.

cheers.

=== usesguid.rb ===

require 'uuidtools'

module ActiveRecord
  module Usesguid
    def self.included(base) # :nodoc:
      super
      base.extend ClassMethods
    end


    module ClassMethods
      def after_initialize
        self.id ||= UUID.timestamp_create().to_s.gsub('-','')
      end
    end
  end
end


ActiveRecord::Base.class_eval do
  include ActiveRecord::Usesguid
end
webguy7890 (Guest)
on 2007-06-18 00:55
(Received via mailing list)
actually, instead of extending active record, i just wrote an helper
instead by following the instructions here:
http://codesnipers.com/?q=node/143&title=Using-UUI...
This topic is locked and can not be replied to.