# Mixin problems

**URL:** <https://www.ruby-forum.com/t/mixin-problems/110902>\
**Category:** Rails\
**Created:** [July 31, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/mixin-problems/110902 "2007-07-31T22:40:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 31, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/mixin-problems/110902/1 "2007-07-31T22:40:22Z")

</div>

I’m trying to add a mixin i wrote (ModelExtensions module in  
lib/model\_extensions.rb) to ActiveRecord::Base so that i can use the  
methods in it in all my classes. (well actually there’s only a single  
method in there at the moment)

I’ve added this to environment.rb (right at the bottom):

require ‘model\_extensions’  
ActiveRecord::Base.send(:extend, ModelExtensions)

And the web server starts ok. But the mixin just doesn’t seem to work -  
I get a “missing method” message.

So, i explicitly added the mixin to the required classes, with

require ‘model\_extensions’

at the top of the model file. Still no joy. Modules in lib should work  
shouldn’t they? Can anyone see what i’m doing wrong?

thanks in advance

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 1, 2007, 5:23am UTC](https://www.ruby-forum.com/t/mixin-problems/110902/2 "2007-08-01T05:23:17Z")

</div>

On 7/31/07, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m trying to add a mixin i wrote (ModelExtensions module in  
> lib/model\_extensions.rb) to ActiveRecord::Base so that i can use the  
> methods in it in all my classes. (well actually there’s only a single  
> method in there at the moment)
> 
> I’ve added this to environment.rb (right at the bottom):
> 
> require ‘model\_extensions’  
> ActiveRecord::Base.send(:extend, ModelExtensions)

Is the method supposed to be a class method or an instance method? If  
it’s supposed to be and instance method, try include instead of  
extend:

ActiveRecord::Base.send(:include, ModelExtensions)

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 1, 2007, 11:07am UTC](https://www.ruby-forum.com/t/mixin-problems/110902/3 "2007-08-01T11:07:55Z")

</div>

David C. wrote:

> Is the method supposed to be a class method or an instance method? If  
> it’s supposed to be and instance method, try include instead of  
> extend:
> 
> ActiveRecord::Base.send(:include, ModelExtensions)

That’s fixed it! Thanks very much - i didn’t appreciate the subtleties  
when i blindly copied someone’s example code (a recurring problem for me  
with learning rails actually).

cheers!

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [August 1, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/mixin-problems/110902/4 "2007-08-01T17:59:48Z")

</div>

On 8/1/07, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> when i blindly copied someone’s example code (a recurring problem for me  
> with learning rails actually).
> 
> cheers!

Cool - glad to help.

Good luck!  
David
