Class: PersistedValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/common_repository_model/collection.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/common_repository_model/collection.rb', line 8

def validate_each(record, attribute, value)
  is_valid = true
  is_valid = false if value.nil?
  begin
    is_valid = false unless value.persisted?
  rescue NoMethodError
    is_valid = false
  end

  record.errors.add(attribute, 'must be persisted') unless is_valid
  is_valid
end