I have AMOUNT field that ActiveRecord maps to Float.
Then I use text_field helper to generate text inputs on forms.
Helper takes model object and attribute name and does not provide any
formatting abilities, thus 2.40 is shown as 2.4 which is fine with
floats but does not look good with currency.
I have worked around this issue by adding amount_f attribute to the
model that returns formatted string.
I do not like this “solution” for two reasons:
- formatting does not belong to models
- for every Float attribute I am forced to create another read/write
attribute with identical logic.
So this does not look right. What else?
I have tried to override #to_s on Float attribute but Float is a
singleton and overriding is not allowed.
I have tried to inherit from Float and override it there but did not get
it to work either.
Can someone post an elegant and clean solution for this problem?