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, -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Fernando,I suggest you look at http://apidock.com/rails/ActionView/Helpers/NumberHelper/number_to_currency. Call the helper with value.to_f * / 100 James. 2009/3/22 Fernando Perez <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>> > 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, > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
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 priceshould 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 Perez <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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, > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
> or however you want to manually manage the accessors, just make sure > when overloading you use the read_attribute, and write_attributeThat''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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---