# Mixins?

**URL:** <https://www.ruby-forum.com/t/mixins/93821>\
**Category:** Rails\
**Created:** [April 11, 2006, 1:47pm UTC](https://www.ruby-forum.com/t/mixins/93821 "2006-04-11T13:47:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![seanb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c0e974/32.png) [@seanb](https://www.ruby-forum.com/u/seanb)\
**Post date:** [April 11, 2006, 1:47pm UTC](https://www.ruby-forum.com/t/mixins/93821/1 "2006-04-11T13:47:30Z")

</div>

As is often the case when I tackle a new platform/language, I get the  
big  
picture very quickly (because frameworks are frameworks are frameworks)  
but  
its the nitty-gritty of the language that bogs me down…

So I have some similar methods on a few of my model classes that I  
wanted to  
push into a helper. Now I reckoned that the Ruby way was to create a  
module  
and mix it in with include. However, I can’t get it to work

In the first instance I put it straight into the models directory:

module FieldHelper  
def validate\_field  
if template\_field.mandatory? && (!field\_value ||  
field\_value.length==0)  
errors.add(template\_field.name, \_(‘cant.be.blank’))  
end  
end  
end

Now in the model I have this :

require “field\_helper”

class SchemeAccountField \< ActiveRecord::Base  
include FieldHelper

protected  
def validate  
validate\_field  
end

end

So basically I want to mix in very similar validation rules.

However, what I get is:

undefined local variable or method `validate\_field’ for …

I’m obviously missing something obvious and/or being stupid but that’s  
the  
nature of the game with a new language I suppose.

Any help out there?

---

<div class="post-metadata">

**Author:** ![seanb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c0e974/32.png) [@seanb](https://www.ruby-forum.com/u/seanb)\
**Post date:** [April 11, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/mixins/93821/2 "2006-04-11T16:15:41Z")

</div>

OK sorted it. I forgot to put the module in a file in the lib  
directory…  
missed that bit. I can now include it in my model classes.
