Allan & Steve are the chubby founders of LessEverything. This is their blog, hear them rant, praise, give advice and talk about Just Stuff, Less Accounting, Lovd by Less, More Honey, Less Memories, Code, Business, Design, Marketing
Chat with Us
August 30th, 2007

Hackilious

written by Steven Bristol

This is a righteous hack:

http://larrytheliquid.com/2007/03/18/validating-positive-with-infinity/

Until you realize that there is a hard limit to number size in the database. That attribute is stored in a particular number of bytes. And this hack does not take that into consideration.

So then you look in the database to see how many bytes make up the column and you start thinking that maybe this is the wrong direction? You’re not checking maximum number values anywhere else (except string lengths). So then you consider that maybe that is why rails makes an integer column 11 bytes by default, that is a really big number.

So then you start writing something like this (because you really need to make sure it is a positive number):

def validate
  errors.add(:price, 'must be positive') if self.price < 0
end

And then you think, maybe I should just use the hack….....

P.S. Consider your self a hard core hacker if at any time while readying this post you found yourself thinking “One man’s brilliant hack is another man’s language feature.” ;)
P.P.S. If you don’t know what the P.S. means, you should immediately start reading 2600 and go back and enjoy the original episodes of The Broken (wasn’t Kevin Rose a cute kid?)

Leave a Reply