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. :)
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. :)
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