-2

As you may be able to see in the image, I have a User model and @user.zip is stored as an integer for validation purposes (ie, so only digits are stored, etc.). I was troubleshooting an error when I discovered that my sample zip code (00100) was automatically being converted to binary, and ending up as the number 64.

Any ideas on how to keep this from happening? I am new to Rails, and it took me a few hours to figure out the cause of this error, as you might imagine :)

I can't imagine any other information would be helpful here, but please inform me if otherwise.

enter image description here

Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
therealrodk
  • 386
  • 2
  • 10
  • You shouldn't store zipcodes as integers – Tamer Shlash Feb 24 '18 at 07:53
  • 2
    Also, DO NOT post pictures of console output. The pictures is not even readable because the font is so bad. Instead, post text. – Tamer Shlash Feb 24 '18 at 07:55
  • @TamerShlash If you click on the image it opens a larger, much clearer version. – therealrodk Feb 24 '18 at 16:32
  • I'd like to thank those who downvoted a perfectly legitimate question. This surely will give other newbies the confidence to speak up. – therealrodk Feb 24 '18 at 16:36
  • The downvotes probably were because you had an image (and it was originally a _link_ to an image) of console output, which is a very bad thing. In your future questions, copy and paste the (relevant) output, don't screenshot it. Nobody here likes screenshots unless it's for visual things. (I didn't downvote btw). – Tamer Shlash Feb 24 '18 at 17:07

2 Answers2

1

This is not binary, this is octal.

In Ruby, any number starting with 0 will be treated as an octal number. You should check the Ruby number literals to learn more about this, here's a quote:

You can use a special prefix to write numbers in decimal, hexadecimal, octal or binary formats. For decimal numbers use a prefix of 0d, for hexadecimal numbers use a prefix of 0x, for octal numbers use a prefix of 0 or 0o, for binary numbers use a prefix of 0b. The alphabetic component of the number is not case-sensitive.

For your case, you should not store zipcodes as numbers. Not only in the database, but even as variables don't treat them as numeric values. Instead, store and treat them as strings.

Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
-1

The zip should probably be stored as a string since you can't have a valid integer with leading zeroes.

cionescu
  • 144
  • 9