Validation to ensure uniqueness of but ignoring empty values?
activerecord, ruby-on-rails, ruby-on-rails-3
Solution
Yes, there are two possible options that you can pass to validations for optional fields: `:allow_blank` or `:allow_nil`, which will skip the validations on `blank` and `nil` fields, respectively. If you change your validation to the following, you should get the behaviour you want:
validates_uniqueness_of :acronym, :allow_blank => true, :scope => [:group_id], :case_sensitive => false
Problem
I have the following model field validation: ``` validates_uniqueness_of :acronym, :scope => [:group_id], :case_sensitive => false ``` The problem is that this field is optional, and a empty/nil value is returning as being an acronym already taken. I only want to validate that an acronym is unique if a value was provided.. Is there a way to update this validation to only take place if there is an actual value.. not nil/empty? Thanks