Allow a "$" in a price field

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

Nick wrote:

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?   

property.price_before_type_case

Great, thanks Jack! That solved it. -Nick

Nick wrote:

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?

property.price_before_type_case

That's: ...before_type_cast (not _case)

Thanks! Nick

-- Jack Christensen jackc@hylesanderson.edu

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 Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com