# Helper methods for ActiveRecord

**URL:** <https://www.ruby-forum.com/t/helper-methods-for-activerecord/79851>\
**Category:** Rails\
**Created:** [March 11, 2006, 11:19am UTC](https://www.ruby-forum.com/t/helper-methods-for-activerecord/79851 "2006-03-11T11:19:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![harm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b4bc9f/32.png) [@harm](https://www.ruby-forum.com/u/harm)\
**Post date:** [March 11, 2006, 11:19am UTC](https://www.ruby-forum.com/t/helper-methods-for-activerecord/79851/1 "2006-03-11T11:19:17Z")

</div>

Hi all,  
I have several models which all have an email field. This email address  
should of course be validated and for that I have this nice regex. But  
now I have the email validation method copied throughout my different  
models. Which is clearly stupid. I figured I needed a helper (module?)  
and use this helper I my models. But I can’t seem to find anyway of  
helpers for models, only for the controllers and views. Am I missing  
something here?  
Any help is greatly appreciated.

Harm

---

<div class="post-metadata">

**Author:** ![harm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b4bc9f/32.png) [@harm](https://www.ruby-forum.com/u/harm)\
**Post date:** [March 11, 2006, 1:32pm UTC](https://www.ruby-forum.com/t/helper-methods-for-activerecord/79851/2 "2006-03-11T13:32:41Z")

</div>

Harm aa wrote:

> Hi all,  
> I have several models which all have an email field. This email address  
> should of course be validated and for that I have this nice regex. But  
> now I have the email validation method copied throughout my different  
> models. Which is clearly stupid. I figured I needed a helper (module?)  
> and use this helper I my models. But I can’t seem to find anyway of  
> helpers for models, only for the controllers and views. Am I missing  
> something here?  
> Any help is greatly appreciated.
> 
> Harm

I’ve put mine in environment.rb as a constant and then just refer to in  
in my model validation.

\_Tony

---

<div class="post-metadata">

**Author:** ![harm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b4bc9f/32.png) [@harm](https://www.ruby-forum.com/u/harm)\
**Post date:** [March 11, 2006, 7:49pm UTC](https://www.ruby-forum.com/t/helper-methods-for-activerecord/79851/3 "2006-03-11T19:49:50Z")

</div>

On Sat, 11 Mar 2006 13:32:41 +0100, Anthony G. wrote:

> Harm aa wrote:
> 
> > But I can’t seem to find anyway of  
> > helpers for models, only for the controllers and views. Am I missing  
> > something here?  
> > Any help is greatly appreciated.
> 
> I’ve put mine in environment.rb as a constant and then just refer to in  
> in my model validation.

For more complicated cases, there are two ways to do it: inherit your  
models from a common class, or put your helper functions into a mixin.  
See:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/135776](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/135776)

Jay L.

---

<div class="post-metadata">

**Author:** ![harm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b4bc9f/32.png) [@harm](https://www.ruby-forum.com/u/harm)\
**Post date:** [March 12, 2006, 11:06am UTC](https://www.ruby-forum.com/t/helper-methods-for-activerecord/79851/4 "2006-03-12T11:06:10Z")

</div>

Excellent! I should have thought of that. Thanks a lot.
