# Modelling money

**URL:** <https://www.ruby-forum.com/t/modelling-money/68855>\
**Category:** Rails\
**Created:** [September 15, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/modelling-money/68855 "2006-09-15T22:01:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![creativetags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@creativetags](https://www.ruby-forum.com/u/creativetags)\
**Post date:** [September 15, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/modelling-money/68855/1 "2006-09-15T22:01:18Z")

</div>

hi

i’m trying to write an app to keep track of expenses and invoices etc.  
for my small business. it’s mainly an excuse to learn ROR but in doing  
so i’ve hit upon a problem: money. I need to store monetary values in  
the database (MySQL) which I believe you would normally use decimal  
type for. My understanding is that rails is going to support that in  
the next version with Active Record but doesn’t currently. Also using  
floats is to be avoided due to rounding errors.

So what does one do in the mean time? I found a money gem  
[http://rubyforge.org/projects/money/](http://rubyforge.org/projects/money/) with no real documentation (I’m  
looking for worked examples to learn from). or a do-it-yourself method  
[ActiveRecord::Aggregations::ClassMethods](http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html)

This seems like such a simple problem I can’t believe I’ve spent so  
long trying to solve it - can somebody please tell me an easy way to do  
this?

thanks, mark

---

<div class="post-metadata">

**Author:** ![creativetags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@creativetags](https://www.ruby-forum.com/u/creativetags)\
**Post date:** [September 15, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/modelling-money/68855/2 "2006-09-15T22:17:19Z")

</div>

I haven’t done it yet, but I just plan on writing a method which will  
convert a string (eg, ‘5.28’) to the number 520, and then another to  
convert back to ‘5.28’. That makes a nice easy way to store money as  
integers.  
Hope this helps.  
-Nathan

---

<div class="post-metadata">

**Author:** ![creativetags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@creativetags](https://www.ruby-forum.com/u/creativetags)\
**Post date:** [September 15, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/modelling-money/68855/3 "2006-09-15T22:19:19Z")

</div>

\*sorry, that should be to the number 528.

---

<div class="post-metadata">

**Author:** ![creativetags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@creativetags](https://www.ruby-forum.com/u/creativetags)\
**Post date:** [September 15, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/modelling-money/68855/4 "2006-09-15T22:59:25Z")

</div>

Hi !

2006/9/15, creativetags [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> So what does one do in the mean time? I found a money gem  
> [http://rubyforge.org/projects/money/](http://rubyforge.org/projects/money/) with no real documentation (I’m  
> looking for worked examples to learn from). or a do-it-yourself method  
> [ActiveRecord::Aggregations::ClassMethods](http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html)

Use the Money gem. Then in your model, you simply compose yourself:

class LineItem \< ActiveRecord::Base  
composed\_of :unit\_price, :class\_name =\> ‘Money’, :mapping =\>  
[%w(unit\_price\_cents cents), %w(unit\_price\_currency currency)]  
end

## Hope that helps !

FranÃ§ois Beausoleil  
[http://blog.teksol.info/](http://blog.teksol.info/)  
[http://piston.rubyforge.org/](http://piston.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![creativetags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@creativetags](https://www.ruby-forum.com/u/creativetags)\
**Post date:** [September 15, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/modelling-money/68855/5 "2006-09-15T23:09:26Z")

</div>

thanks for all the suggestions. I guess the problem doesn’t lie in  
outputting a stored integer to the view as thats easy to format as Roy  
Tinker suggested. How do you get a value a user inputs into a form  
field (e.g. £10.23) into a balance variable in the object model?

You could validate in the form (and maybe do some formatting to get it  
to 1023 or something) but this seems risky - what if javascript is  
disabled?

Alternatively you get the form back to the controller and i’m used to  
doing:  
@invoice = Invoice.find(params[:id])  
but if you’re using the Money gem that won’t work will it? you need to  
create a new Money object and pass that into the balance field of  
@invoice

I really want a simple way to do this in the controller (or model)

---

<div class="post-metadata">

**Author:** ![creativetags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@creativetags](https://www.ruby-forum.com/u/creativetags)\
**Post date:** [September 15, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/modelling-money/68855/6 "2006-09-15T23:09:26Z")

</div>

> I haven’t done it yet, but I just plan on writing a method which will  
> convert a string (eg, ‘5.28’) to the number 520, and then another to  
> convert back to ‘5.28’. That makes a nice easy way to store money as  
> integers.

But you also lost the ability to do math directly in the database (at  
least for db’s that distinguish b/n strings and integers)…

I think until Rails supports decimal you should just use the base unit  
(for US - store it in cents) and convert in the views as needed.

---

<div class="post-metadata">

**Author:** ![creativetags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@creativetags](https://www.ruby-forum.com/u/creativetags)\
**Post date:** [September 20, 2006, 1:34pm UTC](https://www.ruby-forum.com/t/modelling-money/68855/7 "2006-09-20T13:34:31Z")

</div>

creativetags wrote:

> I need to store monetary values in the database (MySQL) which I believe you  
> would normally use decimal type for. My understanding is that rails is going  
> to support that in the next version with Active Record but doesn’t currently.

You can use edge Rails. I also needed decimal support but my app had  
tests that  
didn’t pass under edge Rails, so I backported decimal support to current  
stable  
release (1.1.6). If you’re interested in using the backport, I can send  
you the  
patch.

–  
Sava C.

---

<div class="post-metadata">

**Author:** ![creativetags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@creativetags](https://www.ruby-forum.com/u/creativetags)\
**Post date:** [September 15, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/modelling-money/68855/8 "2006-09-15T23:19:27Z")

</div>

I’m using the dollars and cents plugin.

[http://blog.codahale.com/2006/05/18/dollars\_and\_cents-a-rails-plugin/](http://blog.codahale.com/2006/05/18/dollars_and_cents-a-rails-plugin/)

You make your db column an int, and the plugin handles the conversion  
for you, invisibly.

On 9/15/06, creativetags [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

–  
To get the programming fast enough so that it’s not a boat anchor  
[…] is to program less. There are lots of things we can program, but  
just because we can doesn’t mean we should. – Ward Cunningham
