I am trying to save zip codes which are passed in the params as "07306", and "03452", but to_i seems to be converting these string values to 7306 and 3452 before validation, because of which the validation keeps failing.
How do I prevent Ruby from removing the leading zeros?
The zip code is an integer field in the database and the validation checks for the format of the zip using:
validates_format_of :zip, :with => /\A[+\-]?\d+\Z/, :message => "Please enter a valid US zipcode"
It makes conceptually no sense to have a leading 0 when talking about an integer. Either format the zipcode when you use it (ie. make sure it has the right format, add a leading 0 when converting from int to str), or save it as a string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With