Method: ActiveRecord::Base.validates_uniqueness_of_with_nilification
- Defined in:
- lib/common_lib/active_record/base.rb
.validates_uniqueness_of_with_nilification(*args) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/common_lib/active_record/base.rb', line 60 def self.validates_uniqueness_of_with_nilification(*args) # NOTE ANY field that has a unique index in the database NEEDS # to NOT be blank. Multiple nils are acceptable in index, # but multiple blanks are NOT. Nilify ALL fields with # unique indexes in the database. At least those that # would appear on a form, as an empty text box is sent # as '' and not nil, hence the initial problem. # The first one will work, but will fail after. # ONLY IF THE FIELD IS A STRING! class_eval do validates_uniqueness_of args, :allow_blank => true args.each do |arg| before_validation { self.send("#{arg}=", nil) if self.send(arg).blank? } end end end |