Class: UniqueInCollectionValidator

Inherits:
ActiveRecord::Validations::UniquenessValidator
  • Object
show all
Defined in:
app/validators/unique_in_collection_validator.rb

Overview

Validator for attributes that only need to be unique within a collection e.g. blog_post.slug within the month of posting

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/validators/unique_in_collection_validator.rb', line 12

def validate_each( record, attribute, value )
  existing_error_count = record.errors[ attribute ].size
  super
  updated_error_count = record.errors[ attribute ].size

  uniqueness_error_count = updated_error_count - existing_error_count
  return if uniqueness_error_count.zero? # globally unique

  return unless unique_in_collection?( record, attribute, value )

  remove_irrelevant_errors( record, attribute )
end