Modelling money

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/ with no real documentation (I'm looking for worked examples to learn from). or a do-it-yourself method ActiveRecord::Aggregations::ClassMethods

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

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

*sorry, that should be to the number 528.

Hi !

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)

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.

I'm using the dollars and cents 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.

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.