djberg96 ([info]djberg96) wrote,
@ 2009-10-20 14:11:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Current mood: annoyed

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.


Advertisement


(Read 3 comments)

Post a comment in response:

From:
(will be screened)
Help(will be screened)
Identity URL: 
Username:
Password:
Don't have an account? Create one now.
Subject:
No HTML allowed in subject
   Help
Message:

 
Notice! This user has turned on the option that logs your IP address when posting. Help
Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…