# Extending a model outside of its file

**URL:** <https://www.ruby-forum.com/t/extending-a-model-outside-of-its-file/134669>\
**Category:** Rails\
**Created:** [April 13, 2008, 7:42pm UTC](https://www.ruby-forum.com/t/extending-a-model-outside-of-its-file/134669 "2008-04-13T19:42:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 13, 2008, 7:42pm UTC](https://www.ruby-forum.com/t/extending-a-model-outside-of-its-file/134669/1 "2008-04-13T19:42:13Z")

</div>

Hello.

I’m wondering how to add methods to a model (stored in /app/models)  
from a ruby file located in /lib/my-extension for instance. How is it  
possible? Are the models of an app stored in a specific ruby module?

Thanks.

aurels

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 13, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/extending-a-model-outside-of-its-file/134669/2 "2008-04-13T20:24:34Z")

</div>

Aurels wrote:

> I’m wondering how to add methods to a model (stored in /app/models)  
> from a ruby file located in /lib/my-extension for instance. How is it  
> possible? Are the models of an app stored in a specific ruby module?

Classes often must extend in the correct order. Maybe the ./app/models  
version must interpret before the ./lib version. If so, the top of  
./lib/my\_model.rb should say require RAILS\_ROOT +  
‘/app/models/my\_model’, to  
get the correct one.

Otherwise, the top of ./app/models/my\_model could likewise require the  
./lib  
version.

But this opens the question why not put everything in one model?

Either way, the “magic loader” system (aka “autorequire”) will hide your  
app/model version if any interpretation sees the ./lib version first.  
The  
MyModel class name will be satisfied, and not trigger an auto-require.  
You  
must require the explicit app/model version at the top of any .rb file  
that  
you expect to correctly see it.

Now, why are you trying to do this?

–  
Phlip  
[Test Driven Ajax (on Rails) [Book]](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 13, 2008, 8:37pm UTC](https://www.ruby-forum.com/t/extending-a-model-outside-of-its-file/134669/3 "2008-04-13T20:37:26Z")

</div>

Thanks for your reply

> Now, why are you trying to do this?

I want to separate a complicated method outside of the model and to be  
able to “desactivate” it easily…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 14, 2008, 1:24pm UTC](https://www.ruby-forum.com/t/extending-a-model-outside-of-its-file/134669/4 "2008-04-14T13:24:31Z")

</div>

I finally found my way out using a module and an include. It’s not  
completely independent but better than putting everything in the same  
place!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 13, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/extending-a-model-outside-of-its-file/134669/5 "2008-04-13T20:43:18Z")

</div>

More simply, isn’t it possible to define a class in two INDEPENDANT  
files?

e.g.,

file1.rb:  
class A  
def m ; end  
end

file2.rb  
class A  
def n; end  
end

and I want

- A to have the method m if only file1.rb was loaded
- A to have the method n if only file2.rb was loaded
- A to have both m and n if the two files were loaded
