Allow a "$" in a price field


#1

Hi guys. My Property model has a “price” attribute, which is an
integer. When creating a property, I’d like users to be able to submit
“$1234” as the price, and have my app remove the “$”.

I thought this could be accomplished by having a before_validation
callback on the “price” attribute remove the “$”. Unfortunately, by
the time the callback is executed, the submitted price has been cast
to a Fixnum. This means that any price beginning with a “$” is
converted to 0 (zero).

Does anyone have any suggestions for how to do this?

Thanks!
Nick


#2

Nick wrote:

Does anyone have any suggestions for how to do this?

property.price_before_type_case

Thanks!
Nick


Jack C.
removed_email_address@domain.invalid


#3

On Oct 10, 10:26 am, Jack C. removed_email_address@domain.invalid wrote:

property.price_before_type_case

Great, thanks Jack! That solved it.
-Nick


#4

On Oct 10, 2008, at 10:26 AM, Jack C. wrote:

converted to 0 (zero).

Does anyone have any suggestions for how to do this?

property.price_before_type_case

That’s: …before_type_cast (not _case)

Thanks!
Nick


Jack C.
removed_email_address@domain.invalid

You’ll still have to deal with the original string value of the field,
of course, but it seems that’s exactly what you want.

-Rob

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid