Class: Katello::Validators::UniqueFieldInOrg

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/lib/katello/validators/unique_field_in_org.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



6
7
8
9
10
11
12
# File 'app/lib/katello/validators/unique_field_in_org.rb', line 6

def validate_each(record, attribute, value)
  if value
    others = record.class.where(attribute => value).joins(:environment).where("#{Katello::KTEnvironment.table_name}.organization_id" => record.environment.organization_id)
    others = others.where("#{record.class.table_name}.id != ?", record.id) if record.persisted?
    record.errors[attribute] << N_("already taken") if others.any?
  end
end