How to write database specific custom validator in Rails 3.0? -


i need write custom validator check if record exists in database or not. sort of opposite of validate uniqueness, couldn't find achieve wanted in built in validators.

what i'm attempting check if referrer exists or not in users table. if referrer's username "testuser", want check in users table whether "testuser" exists.

i have created custom validator:

class referrerexistsvalidator < activemodel::eachvalidator 

however i'm unsure how proceed fetch details there database, pointers?

write following validation class

 class referrerexistsvalidator < activemodel::eachvalidator      def validate_each(object, attribute, value)        unless user.find_by_username(value)          object.errors[attribute] << (options[:message] || "referrer not exist")        end      end    end  

add following relevant model

validates :referrer_exists => true


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -