#!/usr/bin/env ruby require "active_model" require "active_support/core_ext" # Checks a value not equals to another attribute's one. # # validates :password, not_equals_to: :login # # Use the :allow_nil key to skip validation in case the attribute value isn't set. # # validates :password, not_equals_to: :login, allow_nil: true # class NotEqualsValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) return if value.nil? && options[:allow_nil] if value == record.send(options[:to]) record.errors.add attribute, :not_equals end end end