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 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-15 20:13 UTC
Re: Modelling money
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 On 15/09/06, creativetags <creativetags-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 > 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 > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Sep-15 20:13 UTC
Re: Modelling money
*sorry, that should be to the number 528. On 15/09/06, njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On 15/09/06, creativetags <creativetags-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > 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 > > 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 > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Use integers 100 times larger than the amount and do translation in the views. Example: $12.34 would be stored as 1234 -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org Sent: Friday, September 15, 2006 1:14 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: Modelling money *sorry, that should be to the number 528. On 15/09/06, njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <njmacinnes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On 15/09/06, creativetags <creativetags-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > hi > > > > i''m trying to write an app to keep track of expenses and invoicesetc.> > for my small business. it''s mainly an excuse to learn ROR but indoing> > so i''ve hit upon a problem: money. I need to store monetary valuesin> > 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. Alsousing> > 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-yourselfmethod> >http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethod s.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 todo> > this? > > > > thanks, mark > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Hi ! 2006/9/15, creativetags <creativetags@gmail.com>:> 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 > http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.htmlUse 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://piston.rubyforge.org/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
> 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.> Hope this helps. > -Nathan > > On 15/09/06, creativetags <creativetags-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> 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 >> 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 >> >> >>> >> > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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. On 9/15/06, creativetags <creativetags-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 > 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 > > > > >-- 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
You can optionally do validation in the form for quick feedback, but don''t do formatting. That way if javascript is disabled, it won''t affect what the controller receives. You can use Ruby''s text processing to translate to an integer in the controller, or change it to whatever format the Money gem accepts. -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of creativetags Sent: Friday, September 15, 2006 2:05 PM To: Ruby on Rails: Talk Subject: [Rails] Re: Modelling money 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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 Chankov --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---