I''m storying amounts of money in my database as cents. So I have custom accessors in my model, for example def actual_amount read_attribute("actual_amount").to_f / 100.0 end But when I use <%= text_field ''item'', ''actual_amount'' %> I get, for example, "3600" instead of "36.00". If I ignore the helper, and just put <input type="text" value="<%=@item.actual_amount%>" size="5" name="item[actual_amount]" id="item_actual_amount"/> I get "36.00", as I would expect. Can anybody tell me why? --Al Evans -- Posted via http://www.ruby-forum.com/.
Al Evans wrote:> I''m storying amounts of money in my database as cents. > > So I have custom accessors in my model, for example > > def actual_amount > read_attribute("actual_amount").to_f / 100.0 > end > > But when I use > > <%= text_field ''item'', ''actual_amount'' %> > > I get, for example, "3600" instead of "36.00". > > If I ignore the helper, and just put > > <input type="text" value="<%=@item.actual_amount%>" size="5" > name="item[actual_amount]" id="item_actual_amount"/> > > I get "36.00", as I would expect. > > Can anybody tell me why?AR form helpers use the attribute_before_type_cast method, so you can either define an actual_amount_before_type_cast method (that does not do what its name implies), or read http://thread.gmane.org/gmane.comp.lang.ruby.rails/60977 -- We develop, watch us RoR, in numbers too big to ignore.
Mark Reginald James wrote:> Al Evans wrote: >> <%= text_field ''item'', ''actual_amount'' %> >> Can anybody tell me why? > AR form helpers use the attribute_before_type_cast method, > so you can either define an actual_amount_before_type_cast > method (that does not do what its name implies), or read > http://thread.gmane.org/gmane.comp.lang.ruby.rails/60977Thanks! That''s just the answer I needed. I knew I''d seen something about this before, but I couldn''t remember where, nor could I find it. --Al Evans -- Posted via http://www.ruby-forum.com/.
Reasonably Related Threads
- How can I intercept attribute calls?
- Inserting NULL values into PostgreSQL from Rails
- Does text_field go directly to attributes hash in AR object?
- text_field doesn''t call overridden ActiveRecord getters
- how do I validate currency format if I am storing in cents?