HELP! - number_to_currency in model?

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.

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)

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

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.

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.

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?