# How can my model reference a helper?

**URL:** <https://www.ruby-forum.com/t/how-can-my-model-reference-a-helper/128199>\
**Category:** Rails\
**Created:** [February 5, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/how-can-my-model-reference-a-helper/128199 "2008-02-05T20:41:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![carpeliam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@carpeliam](https://www.ruby-forum.com/u/carpeliam)\
**Post date:** [February 5, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/how-can-my-model-reference-a-helper/128199/1 "2008-02-05T20:41:58Z")

</div>

I know, I know, they’re not allowed to. But here’s my problem. I’ve got  
a really simple model with two fields, name and price, which I want to  
show in a form select drop-down. “collection\_select” takes a  
‘text\_method’ parameter, which is the method in the model that gets its  
value placed in the tag.

Because I want to show both name and price, I had to add an extra method  
in my model-

def description  
“#{name} - #{number\_to\_currency(price)}”  
end

But this fails, because now I’m trying to reference number\_to\_currency  
(a helper method) from my model. How can I get around this?

---

<div class="post-metadata">

**Author:** ![carpeliam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@carpeliam](https://www.ruby-forum.com/u/carpeliam)\
**Post date:** [February 5, 2008, 9:03pm UTC](https://www.ruby-forum.com/t/how-can-my-model-reference-a-helper/128199/2 "2008-02-05T21:03:45Z")

</div>

Hi,

> But this fails, because now I’m trying to reference number\_to\_currency  
> (a helper method) from my model. How can I get around this?

Well… as you said it’s not the best idea since you are somehow breaking  
the rules of MVC by using methods which are supposed to be only used by  
the views.

That said, a helper is just a ruby module, meaning you can use it in any  
class you want if you just include it. The only problem you can find is  
that some helper methods will assume they are running in a normal  
controller-view cycle (as they should), and they will try to use some of  
the magic variables that get instantiated automatically behind the  
scenes (such as the request, for example).

In case the helper you want to use doesn’t try to use any of those  
variables, you should be able to just include the helper module in your  
model and use all the included methods directly.

regards,

javier ramírez

---

<div class="post-metadata">

**Author:** ![carpeliam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@carpeliam](https://www.ruby-forum.com/u/carpeliam)\
**Post date:** [February 5, 2008, 9:09pm UTC](https://www.ruby-forum.com/t/how-can-my-model-reference-a-helper/128199/3 "2008-02-05T21:09:46Z")

</div>

Can’t you just add the number\_to\_currency in the view? Keep things  
simple.

---

<div class="post-metadata">

**Author:** ![carpeliam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@carpeliam](https://www.ruby-forum.com/u/carpeliam)\
**Post date:** [February 5, 2008, 9:11pm UTC](https://www.ruby-forum.com/t/how-can-my-model-reference-a-helper/128199/4 "2008-02-05T21:11:38Z")

</div>

If you can come up with a way to integrate that with collection\_select,  
I’m all ears. 😉 But collection\_select seems to reference the model  
directly for display, which kind of breaks MVC.

Melvin R. wrote:

> Can’t you just add the number\_to\_currency in the view? Keep things  
> simple.

---

<div class="post-metadata">

**Author:** ![carpeliam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@carpeliam](https://www.ruby-forum.com/u/carpeliam)\
**Post date:** [February 5, 2008, 10:37pm UTC](https://www.ruby-forum.com/t/how-can-my-model-reference-a-helper/128199/5 "2008-02-05T22:37:50Z")

</div>

Go crazy and add number\_to\_currency to FixNum.

Tehe. Probably dangerous, but still much fun!

-Ryan

On Feb 5, 2:11 pm, Liam M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![carpeliam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e19adc/32.png) [@carpeliam](https://www.ruby-forum.com/u/carpeliam)\
**Post date:** [February 5, 2008, 9:09pm UTC](https://www.ruby-forum.com/t/how-can-my-model-reference-a-helper/128199/6 "2008-02-05T21:09:40Z")

</div>

Agreed, I’d rather not break MVC, which is why I’m a little frustrated  
with collection\_select for referencing the _model_ for what should be  
displayed in the select box… _grumble_

In any case, thanks very much, your solution works.

Liam

javier ramirez wrote:

> Hi,
> 
> > But this fails, because now I’m trying to reference number\_to\_currency  
> > (a helper method) from my model. How can I get around this?
> 
> Well… as you said it’s not the best idea since you are somehow breaking  
> the rules of MVC by using methods which are supposed to be only used by  
> the views.
> 
> That said, a helper is just a ruby module, meaning you can use it in any  
> class you want if you just include it. The only problem you can find is  
> that some helper methods will assume they are running in a normal  
> controller-view cycle (as they should), and they will try to use some of  
> the magic variables that get instantiated automatically behind the  
> scenes (such as the request, for example).
> 
> In case the helper you want to use doesn’t try to use any of those  
> variables, you should be able to just include the helper module in your  
> model and use all the included methods directly.
> 
> regards,
> 
> javier ramï¿½rez
