# Clean way to handle prices

**URL:** <https://www.ruby-forum.com/t/clean-way-to-handle-prices/163562>\
**Category:** Rails\
**Created:** [March 22, 2009, 1:44am UTC](https://www.ruby-forum.com/t/clean-way-to-handle-prices/163562 "2009-03-22T01:44:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [March 22, 2009, 1:44am UTC](https://www.ruby-forum.com/t/clean-way-to-handle-prices/163562/1 "2009-03-22T01:44:31Z")

</div>

Hi,

Currently I am storing my prices as cents. I have one major issue, it is  
the way I convert this value into a decimal looking price. This is done  
easily with to\_f and \*100).to\_i, but it looks ugly and clutters my  
controllers specially when updating the record. I tried with callbacks,  
but it doesn’t do the trick if validation fails, as it returns the raw  
values, i.e in cents.

I tried redefining price and price=, but it caused a stack too deep  
problem with infinite self referential.

What’s the consensus on that? Use the Money gem? I expected to have a  
nice rubyish solution to that problem.

Best regards,

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [March 22, 2009, 8:39am UTC](https://www.ruby-forum.com/t/clean-way-to-handle-prices/163562/2 "2009-03-22T08:39:58Z")

</div>

Fernando,I suggest you look at  
[number\_to\_currency (ActionView::Helpers::NumberHelper) - APIdock](http://apidock.com/rails/ActionView/Helpers/NumberHelper/number_to_currency).  
Call the helper with value.to\_f \* / 100

James.

2009/3/22 Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [March 22, 2009, 9:22am UTC](https://www.ruby-forum.com/t/clean-way-to-handle-prices/163562/3 "2009-03-22T09:22:40Z")

</div>

james already mentioned the best way to do this, but if that doesn’t  
match your requirements, just write your own helper-method. stuff like  
that is not done inside the controller.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [March 22, 2009, 10:17am UTC](https://www.ruby-forum.com/t/clean-way-to-handle-prices/163562/4 "2009-03-22T10:17:38Z")

</div>

As someone else mentioned, wrapping this conversion to dollars makes  
it a lot easier to manage.

The stack too deep problem in your overloading of price and price=  
should be able to be solved by using the direct accesors like so:

def price  
cents\_to\_dollars(read\_attribute(:price))  
end

def price=(value)  
write\_attribute(:price, dollars\_from\_cents(value))  
end

or however you want to manually manage the accessors, just make sure  
when overloading you use the read\_attribute, and write\_attribute,  
because if you simply use the variable as normally, you will be  
calling the same method you are defining, causing the stack too deep  
error.

On Mar 22, 1:44 pm, Fernando P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [March 22, 2009, 2:07pm UTC](https://www.ruby-forum.com/t/clean-way-to-handle-prices/163562/5 "2009-03-22T14:07:08Z")

</div>

> or however you want to manually manage the accessors, just make sure  
> when overloading you use the read\_attribute, and write\_attribute

That’s exactly what I was looking for! I couldn’t find these methods in  
railsbrain because they are private, therefore no documentation is  
generated except if you read the very long text at ActiveRecord::Base.

Thank you very much.
