# Need some basic help with on-the-run model extension

**URL:** <https://www.ruby-forum.com/t/need-some-basic-help-with-on-the-run-model-extension/117940>\
**Category:** Rails\
**Created:** [October 22, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/need-some-basic-help-with-on-the-run-model-extension/117940 "2007-10-22T17:08:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 22, 2007, 5:08pm UTC](https://www.ruby-forum.com/t/need-some-basic-help-with-on-the-run-model-extension/117940/1 "2007-10-22T17:08:52Z")

</div>

Hi all

I want to create a method that - when called in a model definition -  
adds some more methods to this model.

def MyModel \< ActiveRecord::Base  
add\_some\_methods # This should add some methods  
end

So I began with the following code…

def ActiveRecord::Base  
private  
def self.add\_some\_methods  
# Add some methods…  
end  
end

But now I don’t know how I can add those methods in the add\_some\_method  
method. Any help is highly appreciated. 🙂  
Ah yeah, and by the way: how can I determine the actual model’s class  
name in the add\_some\_methods method?

Thanks a lot  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 22, 2007, 11:39pm UTC](https://www.ruby-forum.com/t/need-some-basic-help-with-on-the-run-model-extension/117940/2 "2007-10-22T23:39:00Z")

</div>

Thanks a lot, this was very helpful. 🙂

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 22, 2007, 10:30pm UTC](https://www.ruby-forum.com/t/need-some-basic-help-with-on-the-run-model-extension/117940/3 "2007-10-22T22:30:10Z")

</div>

On 10/22/07, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So I began with the following code…
> 
> def ActiveRecord::Base  
> private  
> def self.add\_some\_methods  
> # Add some methods…  
> end  
> end
> 
> But now I don’t know how I can add those methods in the add\_some\_method  
> method. Any help is highly appreciated. 🙂

Several ways:

> 

class ActiveRecord::Base  
private  
def self.add\_some\_methods  
# using define method  
define\_method(:new\_meth1) {|arg| #code for method body here}  
# using class\_eval  
class\_eval(\<\<-HERE  
def new\_meth2(arg)  
# Code for new\_meth2  
end  
HERE  
end  
end

You might want to check out Module#alias\_method\_chain which is added  
by ActiveSupport, this is the idiomatic way to add extensions to  
existing methods. Lets’ say you wanted to extend  
ActiveRecord::Base#find with some new feature called foo. You’d do  
something like this inside your add\_some\_methods method

```
alias_method_chain :find, :foo

```

and define your extension method as :find\_with\_foo, you can get at the  
original find method by calling :find\_without\_foo since the alias  
method chain expands to:

```
 alias_method :find_without_foo, :find
 alias_method :find, :find_with_foo

```

> Ah yeah, and by the way: how can I determine the actual model’s class  
> name in the add\_some\_methods method?

Just invoke the Module#name method in your class method, either  
with self or no receiver.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
