Class: Citier::InstanceMethods::CitierUniquenessValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/citier/instance_methods.rb

Overview

USAGE validates :attribute, :citier_uniqueness => true Needed because validates :attribute, :uniqueness => true Won’t work because it tries to call child_class.attribute on parents table

Instance Method Summary collapse

Instance Method Details

#validate_each(object, attribute, value) ⇒ Object



28
29
30
31
32
33
# File 'lib/citier/instance_methods.rb', line 28

def validate_each(object, attribute, value)
  existing_record = object.class.where(attribute.to_sym => value).limit(1).first
  if existing_record && existing_record.as_root != object.as_root #if prev record exist and it isn't our current obj
        object.errors[attribute] << (options[:message] || "has already been taken.")  
  end 
end