# How make utilities for models and controllers?

**URL:** <https://www.ruby-forum.com/t/how-make-utilities-for-models-and-controllers/119146>\
**Category:** Rails\
**Created:** [November 3, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/how-make-utilities-for-models-and-controllers/119146 "2007-11-03T20:20:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 3, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/how-make-utilities-for-models-and-controllers/119146/1 "2007-11-03T20:20:19Z")

</div>

It’s me again.

I need to abstract out some utilities that I want accessible to all  
controllers, AR models, and my own non-AR models.

Adding methods to application.rb doesn’t do it – which I think I  
understand because that’s the top of the controller hierarchy, so  
models won’t see controller methods. Plus, these should be easily  
transportable to any application, so cut & paste in application.rb  
would be lame anyway.

I understand Ruby’s concept of modules & mixins, but I come from a  
language where ‘require’ just isn’t used, so still getting used to  
that process.

So, if I create a module where does it go (wherever I want? typically  
in /lib/?), and is it necessary that every model & controller I want  
to use that with, I have to add a “require ‘myModule’” line? There’s  
no way to “load” it for universal usage?

Related…

I tried using request.remote\_ip in a method in an AR model, and am  
getting a method undefined. I haven’t really figured out when I need  
to use Class::Blah.method syntax for stuff. Is that what I need to do  
here? I tried poking around the RDocs for hints, but my guesses  
aren’t working.

Thanks for being my “senior developer pairing buddy” 🙂

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 3, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/how-make-utilities-for-models-and-controllers/119146/2 "2007-11-03T20:27:20Z")

</div>

On 3 Nov 2007, at 19:19, Greg W. wrote:

> So, if I create a module where does it go (wherever I want? typically  
> in /lib/?), and is it necessary that every model & controller I want  
> to use that with, I have to add a “require ‘myModule’” line? There’s  
> no way to “load” it for universal usage?

not require. You want to include the module in the relevant clases, ie

class MyModel \< AR::Base  
include Utilities  
end

(rails would automatically require utilities.rb when it saw Utilities).

You could include such a module in both application.rb and AR::Base  
and wherever else they were useful. I would however query whether this  
is the best strategy - perhaps your utilities belong somewhere else,  
eg if some of your utilities are handy things to do with strings then  
you might be better off extending the String class.

> Related…
> 
> I tried using request.remote\_ip in a method in an AR model, and am  
> getting a method undefined. I haven’t really figured out when I need  
> to use Class::Blah.method syntax for stuff. Is that what I need to do  
> here? I tried poking around the RDocs for hints, but my guesses  
> aren’t working.

You can’t do that. From the model (unless it was passed in to the  
model from the controller) there is no request object.

Fred

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 3, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/how-make-utilities-for-models-and-controllers/119146/3 "2007-11-03T21:30:00Z")

</div>

On Nov 3, 2007, at 12:26 PM, Frederick C. wrote:

> class MyModel \< AR::Base  
> strings then you might be better off extending the String class.  
> its a custom logging/debugging system to trace application steps and  
> state, so I want to be able to trace details of what’s happening  
> inside controllers and models using the same central system. It  
> doesn’t really extend anything. It’s my own system I want to be easy  
> to drop into any app I work on.

There may be other systems to use, but if I do my own that I am  
accustomed to on other platforms that’s one less new thing to have to  
worry about right now. It’s tiny and easily pulled out if I come  
across a better way.

> model from the controller) there is no request object.  
> Yep. Makes sense.

– gw

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 3, 2007, 10:03pm UTC](https://www.ruby-forum.com/t/how-make-utilities-for-models-and-controllers/119146/4 "2007-11-03T22:03:56Z")

</div>

Rails uses a built in logger that is very nice and easy to use. It is  
almost identical to log4java. It’s easy to use and supports various  
logging levels. You can set / alter the default logging levels per  
environment in the various environment files. Examples:

logger.debug “blah blah”  
logger.info “a = #{a.inspect}”  
logger.error “I just blew up”

You may still prefer to use yours, but this is built in to rails and  
logger is automatically available in all of your models, controllers  
and helpers. It is also very easy to create additional log files using  
the same logger.

-Bill

---

<div class="post-metadata">

**Author:** ![S-gw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@S-gw](https://www.ruby-forum.com/u/S-gw)\
**Post date:** [November 3, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/how-make-utilities-for-models-and-controllers/119146/5 "2007-11-03T22:16:12Z")

</div>

On 3 Nov 2007, at 20:29, Greg W. wrote:

> its a custom logging/debugging system to trace application steps and  
> state, so I want to be able to trace details of what’s happening  
> inside controllers and models using the same central system. It  
> doesn’t really extend anything. It’s my own system I want to be easy  
> to drop into any app I work on.

If it’s truly to be used everywhere, you could always include your  
module into Object

Fred
