Forum: Ruby on Rails Allow a "$" in a price field

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick (Guest)
on 2008-10-10 15:10
(Received via mailing list)
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
89441a6c74647d292f5cc951eae24cfa?d=identicon&s=25 Jack Christensen (Guest)
on 2008-10-10 16:27
(Received via mailing list)
Nick wrote:
> Does anyone have any suggestions for how to do this?
>
property.price_before_type_case
> Thanks!
> Nick
> >
>
>


--
Jack Christensen
jackc@hylesanderson.edu
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick (Guest)
on 2008-10-10 18:19
(Received via mailing list)
On Oct 10, 10:26 am, Jack Christensen <ja...@hylesanderson.edu> wrote:
> property.price_before_type_case

Great, thanks Jack! That solved it.
-Nick
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-10-10 18:51
(Received via mailing list)
On Oct 10, 2008, at 10:26 AM, Jack Christensen 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 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
This topic is locked and can not be replied to.