Sha256: ce2ffc53ee09370d0d5a7fe13fbc7d418c7e8ba4e7d65385928ad69f4a9feb1f
Contents?: true
Size: 712 Bytes
Versions: 35
Compression:
Stored size: 712 Bytes
Contents
# A custom validator "set". It's similar to :presence validator # Unlike :presence, :set doesn't care whether the associated record is valid or not, just that it is present # Also unlike :presence, it only works on belongs_to class SetValidator < ActiveModel::EachValidator def validate_each( record, attribute, value) association = record.class.reflect_on_association( attribute ) if association.nil? || !association.belongs_to? raise ArgumentError, "Cannot validate existence on #{record.class} #{attribute}, not a :belongs_to association" end if record.send( attribute ).nil? record.errors.add( attribute, "is not set") end end end
Version data entries
35 entries across 35 versions & 3 rubygems