Rails 3: How do I validate to allow blanks ("") but not nil (NULL in database)

You can try doing:

validates :my_string, exclusion: { in: [nil]}

It is part of the validations themselves in ActiveRecord.

I've tried the others and all of them are very compliated or allow nil too.


This works for me

  validates :my_string, length: { in: 0..255, allow_nil: false }

If you just want to validate that the field is not null, but don't care about blank/empty strings, this works:

  validates :my_string, length: { minimum: 0, allow_nil: false, message: "can't be nil" }