Forum: Ruby on Rails number_to_currency inside model

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
2da252cc1d2fb3701126171831b50a65?d=identicon&s=25 Dave Coleman (pezdude)
on 2006-04-05 04:17
I was wondering if this is possible.

I have a model (Item) that contains products. These products have prices
that I'd like to format using number_to_currency. Rather than doing this
several times inside the view, I thought it might be nice to do it once
inside the model.

The item table has an field called regular_price which holds a currency
amount. I would like to do something like what's below, but I get the
following error message:

undefined method `number_to_currency' for #<Item:0x2479430>

Here is the model.

class Item < ActiveRecord::Base
has_many :item_attributes, :order => 'a_order'

def regular_price

Not sure why it's not finding the number_to_currency helper.

8f3739e3b34bfea19fd381500d8a8fe6?d=identicon&s=25 Anthony Carlos (acarlos)
on 2006-04-05 16:03
(Received via mailing list)

number_to_currency is a Rails method defined in

Here's the link to the doc:

Because it's part of ActionView, it's normally available to View
objects but not Model objects.

I know it's not the answer for which you're hoping. Some might argue
that a function that displays a formatted value belongs in the View
layer rather than the Model layer. Others might argue against this!
It would be nice to hear what people think about stuff like this.

Hope it helps,

This topic is locked and can not be replied to.