# HELP! - number\_to\_currency in model?

**URL:** <https://www.ruby-forum.com/t/help-number-to-currency-in-model/65493>\
**Category:** Rails\
**Created:** [August 18, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/help-number-to-currency-in-model/65493 "2006-08-18T18:13:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@cman](https://www.ruby-forum.com/u/cman)\
**Post date:** [August 18, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/help-number-to-currency-in-model/65493/1 "2006-08-18T18:13:05Z")

</div>

Hi,

I’m trying to use number\_to\_currency in one of my models, but i’m  
getting the  
following error:

undefined method `number\_to\_currency’ for #Product:0x396d680

Obviously, my Product model can’t access number\_to\_currency – is there  
something I need to do to make it available?

Thanks so much for your assistance.

---

<div class="post-metadata">

**Author:** ![cman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@cman](https://www.ruby-forum.com/u/cman)\
**Post date:** [August 18, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/help-number-to-currency-in-model/65493/2 "2006-08-18T18:14:55Z")

</div>

The “number” helpers are only available in your view.

Cheers,  
M Damon H.  
Project Manager  
IFWORLD, Inc.  
479-582-5100 (p)  
479-582-5599 (f)

> **[Home | IFWORLD, INC.](http://ifworld.com/)**
>
> About Ifworld IFWORLD is a technology solutions company whose goal is to provide reliable “one-stop-shopping” solutions targeting the needs of Northwest Arkansas and beyond. We provide virtualization, web hosting, disaster recovery, ISP services, and...

“…as we’re sung to sleep by philosophies that sing save the trees and  
kill  
the children…” – while you were sleeping (casting crowns)

---

<div class="post-metadata">

**Author:** ![cman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@cman](https://www.ruby-forum.com/u/cman)\
**Post date:** [August 18, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/help-number-to-currency-in-model/65493/3 "2006-08-18T18:17:56Z")

</div>

As previously stated, those are view helpers. Models really shouldn’t  
have  
any formatting data in them at all… your view layer should do that for  
you.

But explain why you want to do number\_to\_currency in the model (remember  
this renders a string) and there may be a way to solve your problem. You  
_can_ get number\_to\_currency working in the model – I’ve done it before  
but  
it’s not a good approach.

---

<div class="post-metadata">

**Author:** ![cman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@cman](https://www.ruby-forum.com/u/cman)\
**Post date:** [August 18, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/help-number-to-currency-in-model/65493/4 "2006-08-18T19:21:01Z")

</div>

Your helper approach is the way to go actually. That’s what helpers are  
for.

But make it a little easier to use by just explicitly returning the  
sring at  
the end of the mod

def get\_price\_range(model)  
if model.min == model.max  
price\_range\_text = number\_to\_currency(model.max)  
else  
price\_range\_text = number\_to\_currency(model.min)+ ’ - ’ +  
number\_to\_currency(model.max)  
end  
price\_range\_text  
end

Then your helper works like

Price range: \<%=get\_price\_range @product %\>

Perfectly appropriate. Way to go.

---

<div class="post-metadata">

**Author:** ![cman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/cdc98d/32.png) [@cman](https://www.ruby-forum.com/u/cman)\
**Post date:** [August 18, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/help-number-to-currency-in-model/65493/5 "2006-08-18T18:26:00Z")

</div>

Brian H. wrote:

> As previously stated, those are view helpers. Models really shouldn’t  
> have  
> any formatting data in them at all… your view layer should do that for  
> you.
> 
> But explain why you want to do number\_to\_currency in the model (remember  
> this renders a string) and there may be a way to solve your problem. You  
> _can_ get number\_to\_currency working in the model – I’ve done it before  
> but  
> it’s not a good approach.

Thanks - I’m fairly new to rails, so maybe i’m going about this in the  
wrong way.

I have a Product model with min\_price and max\_price properties.

In many places throughout the site, I need to display a price range. I  
had this in my application\_helper:

def get\_price\_range(min,max)  
if min == max  
price\_range\_text = number\_to\_currency(max)  
else  
price\_range\_text = number\_to\_currency(min)+ ’ - ’ +  
number\_to\_currency(max)  
end  
end

But I thought it would be nice to make this an accesible value on my  
Product model, so in my view I could just say \<%=product.price\_range%\>  
as opposed to \<%=get\_price\_range(product.min\_price,product.max\_price)%\>

So I tried adding this to my Product Model:  
def price\_range  
if self.min\_price == self.max\_price  
price\_range\_text = number\_to\_currency(self.max\_price)  
else  
price\_range\_text = number\_to\_currency(self.min\_price)+ ’ - ’ +  
number\_to\_currency(self.max\_price)  
end  
end

But apparently I shouldn’t be doing this?
