# Extending activerecord base with after\_initialize

**URL:** <https://www.ruby-forum.com/t/extending-activerecord-base-with-after-initialize/107030>\
**Category:** Rails\
**Created:** [June 16, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/extending-activerecord-base-with-after-initialize/107030 "2007-06-16T21:07:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![webguy7890](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e47774/32.png) [@webguy7890](https://www.ruby-forum.com/u/webguy7890)\
**Post date:** [June 16, 2007, 9:07pm UTC](https://www.ruby-forum.com/t/extending-activerecord-base-with-after-initialize/107030/1 "2007-06-16T21:07:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![webguy7890](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e47774/32.png) [@webguy7890](https://www.ruby-forum.com/u/webguy7890)\
**Post date:** [June 17, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/extending-activerecord-base-with-after-initialize/107030/2 "2007-06-17T22:55:18Z")

</div>

actually, instead of extending active record, i just wrote an helper  
instead by following the instructions here:

> **[Caseysoftware](https://caseysoftware.com/?q=node%2F143&title=Using-UUID%2FGUID-as-Primary-Key-in-Rails)**
>
> Caseysoftware -
