Class: Reins::Model::Validations::UniquenessValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/reins/model/validations.rb

Instance Method Summary collapse

Methods inherited from Validator

#initialize

Constructor Details

This class inherits a constructor from Reins::Model::Validations::Validator

Instance Method Details

#validate(record, errors) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/reins/model/validations.rb', line 99

def validate(record, errors)
  value = value_for(record)
  return if value.nil?

  scope = @model.where(@attr => value)
  scope = scope.where("#{@model.primary_key} != ?", record.id) if record.persisted?
  errors.add(@attr, "has already been taken") if scope.first
end