# Where to I put code accessible in all model?

**URL:** <https://www.ruby-forum.com/t/where-to-i-put-code-accessible-in-all-model/174888>\
**Category:** Rails\
**Created:** [September 3, 2009, 3:48pm UTC](https://www.ruby-forum.com/t/where-to-i-put-code-accessible-in-all-model/174888 "2009-09-03T15:48:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AppleII717](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AppleII717](https://www.ruby-forum.com/u/AppleII717)\
**Post date:** [September 3, 2009, 3:48pm UTC](https://www.ruby-forum.com/t/where-to-i-put-code-accessible-in-all-model/174888/1 "2009-09-03T15:48:32Z")

</div>

Even though I’ve been “playing” with RoR since before 1.0, I’ve never  
got a good grasp on where you put code!

Code or helper method placed in helpers can only be called from views,  
if I’m not mistaken.

Code that is specific to a model or controller is also a no-brainer -  
but what about code that you want to share in different models/  
controllers.

What if I have a function unformatPhone that I want to call in a  
model’s before\_save event - where to I stick it so that it can be  
called by all models and maybe even from a view?

Is my only choice in the application\_controller? Is that a good  
choice?

The unformatPhone example may be trivial since it is only a regexp  
replace, but what if it is something more complex. I looked around for  
something that would set the case of proper names and found something  
on github ([GitHub - tenderlove/namecase: Properly case people's names](http://github.com/tenderlove/namecase/tree/master)) which is  
a class. Where do I stick that?

If anyone can point me to a good tutorial on where you put code, I’d  
be thankful.

---

<div class="post-metadata">

**Author:** ![AppleII717](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AppleII717](https://www.ruby-forum.com/u/AppleII717)\
**Post date:** [September 3, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/where-to-i-put-code-accessible-in-all-model/174888/2 "2009-09-03T16:06:03Z")

</div>

AppleII717 wrote:

> Even though I’ve been “playing” with RoR since before 1.0, I’ve never  
> got a good grasp on where you put code!
> 
> Code or helper method placed in helpers can only be called from views,  
> if I’m not mistaken.

Basically right.

> Code that is specific to a model or controller is also a no-brainer -  
> but what about code that you want to share in different models/  
> controllers.
> 
> What if I have a function unformatPhone that I want to call in a  
> model’s before\_save event - where to I stick it so that it can be  
> called by all models and maybe even from a view?

Well, first of all, you should call it unformat\_phone. This isn’t Java.

> Is my only choice in the application\_controller? Is that a good  
> choice?

That won’t work at all. You should put it in the model.

To share code between models, you have the same two choices that you  
have for sharing code between _any_ classes (remember, there’s nothing  
magical about models):

1. Put it in a module and include it in the models you want to use it,  
or
2. Create a subclass of ActiveRecord, put the code there, and have the  
models inherit from that.

[…]

> If anyone can point me to a good tutorial on where you put code, I’d  
> be thankful.

You don’t need a tutorial here, I think. You just need to remember that  
inheritance and module inclusion are fundamental Ruby tricks that should  
be on your radar.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![AppleII717](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AppleII717](https://www.ruby-forum.com/u/AppleII717)\
**Post date:** [September 3, 2009, 4:44pm UTC](https://www.ruby-forum.com/t/where-to-i-put-code-accessible-in-all-model/174888/3 "2009-09-03T16:44:09Z")

</div>

On Sep 3, 3:06 pm, Marnen Laibow-Koser \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> To share code between models, you have the same two choices that you  
> have for sharing code between _any_ classes (remember, there’s nothing  
> magical about models):
> 
> 1. Put it in a module and include it in the models you want to use it,  
> or
> 2. Create a subclass of ActiveRecord, put the code there, and have the  
> models inherit from that.

or 3, decide that this code deserves to be in a class of its own.

Fred
