On Oct 27, 2009, at 1:25 AM, Venka Ashtakala wrote:
> I've been running into an issue with a model, that has a float field
> that is used to store dollar amounts. The users are entering in
> their dollar amounts using this format: 2,000 but after the form is
> submitting, the value is being saved as 2.0 instead of 2000.0. By
> the time the value reaches the 'before_validation' or
> 'before_update' method in my model, the value has already been
> converted to 2.0, so, where can I drop some code to remove the
> commas from the input? Since there is no exception being generated
> I'm not sure what code to look at. ( Also I'm not sure if this is a
> rails or hobo issue ).
>
It's actually a Ruby issue - doing '2,000'.to_f in irb yields 2.0.
Your best bet is to override the field setter method to strip out the
comma. So if your field is named some_field, this is what it'd look
like:
def some_field=(v)
v.gsub!(',','') if v.is_a?(String)
write_attribute('some_field', v)
end
Also note that dollar amounts are better stored in decimal columns, as
that avoids rounding issues (3.37 may not have a terminating
representation as a floating-point number, meaning it ends up as
3.36999999 or something).
--Matt Jones
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Hobo
Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/hobousers?hl=en
-~----------~----~----~----~------~----~------~--~---