# Adding a method to ActiveRecord

**URL:** <https://www.ruby-forum.com/t/adding-a-method-to-activerecord/70709>\
**Category:** Rails\
**Created:** [October 2, 2006, 7:40am UTC](https://www.ruby-forum.com/t/adding-a-method-to-activerecord/70709 "2006-10-02T07:40:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [October 2, 2006, 7:40am UTC](https://www.ruby-forum.com/t/adding-a-method-to-activerecord/70709/1 "2006-10-02T07:40:08Z")

</div>

Hi, I’d like to add a generic function to all my models. Basically a  
function that will handle setting/unsetting flags in various models. Is  
there a way I could add this to ActiveRecord’s Base class and then use  
it all throughout my models? Where could I add this and how?

Thanks,

Sam

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [October 2, 2006, 8:08am UTC](https://www.ruby-forum.com/t/adding-a-method-to-activerecord/70709/2 "2006-10-02T08:08:16Z")

</div>

On 10/2/06, Sam D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi, I’d like to add a generic function to all my models. Basically a  
> function that will handle setting/unsetting flags in various models. Is  
> there a way I could add this to ActiveRecord’s Base class and then use  
> it all throughout my models? Where could I add this and how?
> 
> Thanks,
> 
> Sam

In the lib directory create a file and in it use

module ActiveRecord  
class Base  
def my\_method  
end  
end

I think that should be automatically loaded and included (not sure if  
it’s  
completely automagic). Alternatively you can put that in you  
environment.rband it will get included.

I’m sure there are many other ways of doing this though. This may not  
be  
the best one.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [October 2, 2006, 9:04am UTC](https://www.ruby-forum.com/t/adding-a-method-to-activerecord/70709/3 "2006-10-02T09:04:19Z")

</div>

On Oct 2, 2006, at 7:40 AM, Sam D. wrote:

> Hi, I’d like to add a generic function to all my models. Basically a  
> function that will handle setting/unsetting flags in various  
> models. Is  
> there a way I could add this to ActiveRecord’s Base class and then use  
> it all throughout my models? Where could I add this and how?

Just subclass ActiveRecord::Base:

class MyBaseExtension \< ActiveRecord::Base  
self.abstract\_class = true  
# …  
end

and then make all models inherit from MyBaseExtension.

– fxn

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [October 2, 2006, 5:27pm UTC](https://www.ruby-forum.com/t/adding-a-method-to-activerecord/70709/4 "2006-10-02T17:27:18Z")

</div>

There is (also) a toggle on ActiveRecord which is really useful.

[http://ar.rubyonrails.com/classes/ActiveRecord/Base.html#M000399](http://ar.rubyonrails.com/classes/ActiveRecord/Base.html#M000399)

Vish
