Module: Ohm::Model::Validations

Includes:
Ohm::Validations
Included in:
Defined in:
lib/ohm.rb

Instance Method Summary (collapse)

Methods included from Ohm::Validations

#assert, #assert_format, #assert_numeric, #assert_present, #errors, #valid?, #validate

Instance Method Details

- (Object) assert_unique(:name) - (Object) assert_unique([:street, :city])

Validates that the attribute or array of attributes are unique. For this, an index of the same kind must exist.

Overloads:

  • - (Object) assert_unique(:name)

    Validates that the name attribute is unique.

  • - (Object) assert_unique([:street, :city])

    Validates that the :street and :city pair is unique.



292
293
294
295
# File 'lib/ohm.rb', line 292

def assert_unique(attrs)
  result = db.sinter(*Array(attrs).map { |att| index_key_for(att, send(att)) })
  assert result.empty? || !new? && result.include?(id.to_s), [attrs, :not_unique]
end