Extending activerecord base with after_initialize


#1

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


#2

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-UUID/GUID-as-Primary-Key-in-Rails