views:

34

answers:

1

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

+4  A: 

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
Salil
ok, i get it. thanks buddy. :)
siulamvictor
A bit cleaner is `message = Message.exists?(:name => self.name)` as you aren't doing anything with the message instance.
x1a4
thanks much @x1a4
Salil
thanks all of you :)
siulamvictor