# Extending ActiveRecord::Base

**URL:** <https://www.ruby-forum.com/t/extending-activerecord-base/122118>\
**Category:** Rails\
**Created:** [December 4, 2007, 7:10pm UTC](https://www.ruby-forum.com/t/extending-activerecord-base/122118 "2007-12-04T19:10:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![krystal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f08c70/32.png) [@krystal](https://www.ruby-forum.com/u/krystal)\
**Post date:** [December 4, 2007, 7:10pm UTC](https://www.ruby-forum.com/t/extending-activerecord-base/122118/1 "2007-12-04T19:10:48Z")

</div>

So I have a function that I want all my models to have access to. I’ve  
done some reading and looked for pages and they all say you can extend  
ActiveRecord::Base, and then all your models will get the extra  
function. “Easy” thinks I. I must be missing something.

Here is what I’ve tried:

In lib, I created an active\_directory folder and then in there made a  
base.rb file. The file looks like this:

class ActiveRecord::Base  
def some\_function(param)  
do some stuff  
end  
end

Then, in /config/environment.rb I have require ‘active\_record/base.rb’

Well, that blew up spectacularly, and I couldn’t get script/server to  
run. So I renamed it DB/base.rb and changed the require to reflect  
that. Now script/server runs, but when I try to call some\_function from  
one of my models, it complains that ‘method\_missing’: undefined method  
‘some\_function’ for SomeModel:Class (NoMethodError).

I’ve tried doing this for base.rb instead:

module ActiveRecord  
class Base  
def some\_function(param)  
do some stuff  
end  
end  
end

and it still complains.

I even tried the following for base.rb:

module ActiveRecordCommon  
def some\_function(param)  
do some stuff  
end  
end

Then in the /config/environment.rb I did:

require ‘DB/base.rb’  
class ActiveRecord::Base  
include ActiveRecordCommon  
end

And even THAT didn’t work.

I’m all out of ideas. All the examples I’ve seen have been like the  
ones I’ve described here. I know I’m missing something simple, as that  
tends to be the case. Could someone please point it out to me?

Thanks.

---

<div class="post-metadata">

**Author:** ![krystal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f08c70/32.png) [@krystal](https://www.ruby-forum.com/u/krystal)\
**Post date:** [December 4, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/extending-activerecord-base/122118/2 "2007-12-04T20:42:05Z")

</div>

I’m no expert but my guess is that if you’re intending your method to  
be a class method, then you need to prefix the method name with “self.”

–gw
