Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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