Skip to content

Instantly share code, notes, and snippets.

@jeffkreeftmeijer
Created March 7, 2011 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffkreeftmeijer/858816 to your computer and use it in GitHub Desktop.
Save jeffkreeftmeijer/858816 to your computer and use it in GitHub Desktop.
# I have two models: `AllowBlank` and `AllowNil`:
class AllowBlank < ActiveRecord::Base
validates :name, :presence => true, :allow_blank => true
end
class AllowNil < ActiveRecord::Base
validates :name, :presence => true, :allow_nil => true
end
# The `AllowBlank` validations should always pass and the `AllowNil` validations
# should pass on `nil` values, but fail on empty strings (`''`).
# Let's try it:
irb(main):001:0> AllowBlank.new.valid?
=> false
irb(main):002:0> AllowBlank.new(:name => '').valid?
=> false
irb(main):003:0> AllowNil.new.valid?
=> false
irb(main):004:0> AllowNil.new(:name => '').valid?
=> false
# Sad panda. :(
# So, using `allow_blank` should probably not be supported at all, but `allow_nil` should fail on empty strings. Right? ;)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment