Re: Why is FormHelper using value_before_type_cast ?

Francois Beausoleil
Mon, 06 Mar 2006 03:38:08 -0000

2006/3/5, Michael Koziarski
<michael@koziarski.com>:
> On 3/6/06, Francois Beausoleil <francois.beausoleil@gmail.com> wrote:
> > class Customer
> >   def tax_rate
> >     (read_attribute(:tax_rate) * 100.0).to_i
> >   end
> >
> >   def tax_rate=(rate)
> >     write_attribute(:tax_rate, rate / 100.0)
> >   end
> > end
>
> Otherwise the user's data will be 'randomly' changed by the
system.   i.e.
>
> irb(main):001:0> "asdifjasdliofj".to_i
> => 0
> irb(main):002:0> "123,40".to_i
> => 123
>
> So if it didn't use _before_typecast, and the user typed either of
> those values, then when the page was re-rendered,  the values won't
> have been remembered correctly.

How can custom reader/writer methods be accomodated then ?

Thanks !