Text_field weirdness?

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

I get “36.00”, as I would expect.

Can anybody tell me why?

–Al Evans

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/60977


We develop, watch us RoR, in numbers too big to ignore.

Mark Reginald J. 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/60977

Thanks! 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs