Class: EmbedsMany::Child::UniquenessValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/embeds_many/child.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



24
25
26
27
28
# File 'lib/embeds_many/child.rb', line 24

def validate_each(record, attribute, value)
  if record.exists_in_parent? {|item| item.key?(attribute.to_s) && item[attribute.to_s] == value && record.id.to_s != item['id'].to_s }
    record.errors.add attribute, "#{value} is already taken"
  end
end