Forum: Ruby on Rails extending activerecord base with after_initialize

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
webguy7890 (Guest)
on 2007-06-16 23:07
(Received via mailing list)

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.


=== usesguid.rb ===

require 'uuidtools'

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

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

ActiveRecord::Base.class_eval do
  include ActiveRecord::Usesguid
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:
This topic is locked and can not be replied to.