Module: UniqueGenerator::UniqueChecks::AR

Defined in:
lib/unique_generator.rb

Instance Method Summary collapse

Instance Method Details

#unique_field?(field_name) ⇒ Boolean

Returns whether or not the given field is unique to every item.

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
# File 'lib/unique_generator.rb', line 35

def unique_field?(field_name)
  scope = self.class.where(field_name => send(field_name))
  # When we have an id, check for those with a different id.
  if id.present?
    conditions = self.class.arel_table[:id].eq(id).not
    scope = scope.where(conditions)
  end
  scope.empty?
end