Question

can i have a validation which do exactly opposite to validates_uniqueness_of? i.e. i would like to show a error message when the user input is NOT exist in database.

thanks all. :)

Was it helpful?

Solution

No you have to write your own like following.

class Message < ActiveRecord::Base

def validate
  message=Message.find_by_name(self.name)
  self.errors.add :base, "Name must be present" if message.blank?
end


end

EDITED after comment by @x1a4

def validate
  self.errors.add :base, "Name must be present" unless Message.exists?(:name => self.name)
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top