| djberg96 ( @ 2009-10-20 14:11:00 |
| Current mood: |
Back to Rails
So, I'm doing RoR work again, and I'm hitting those annoying little issues that bugged me the first time around.
Today, it's the fact that I have to setup separate validations to handle nils/blanks. IMO I should be able to do this:
validates_numericality_of :elevation, :allow_nil => false
Now, this works. It validates that elevation is a number and it doesn't allow nulls. The problem is that, in the case of a null, it still sets the error message to "not a number". That's not what I want. I want "can't be blank". And I *only* want that error message. To get that effect I have to setup two separate validations, and add a conditional to the second one.
validates_presence_of :elevation
validates_numericality_of :elevation,
:unless => Proc.new{ |site| site.elevation.blank? }
What a hassle.
Update: Anonymous user posted a better solution. See the comments.