Why is FormHelper using value_before_type_cast ?

Francois Beausoleil
Mon, 06 Mar 2006 03:11:41 -0000

Hi !

Just wondering here, because if I define reader methods, then these
aren't used in the forms.

For example, I have this:
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

<%= text_field :customer, :tax_rate %>

For 7.5, the value shown on screen is 750.  Of course, I could do:
<%= text_field :customer, :tax_rate, :value => @customer.tax_rate %>

but that gets pretty boring real quick...

So, what is the rationale behind this decision ?  Are the date/time
helpers the ones that need the value_before_type_cast ?

Thanks !