views:

45

answers:

1

Hi - I'm trying to capture the value that's throwing a uniqueness error (or for that matter, any other type of built-in validation) to display it in the :message option. Here's what I tried (didn't work)

# inside the model
validate_uniqueness_of :name, :message => "#{name} has already been taken" # also tried using #{:name}

I could use a custom validation, but this beats the point of using something that's already integrated into AR. Any thoughts? thanks.

A: 

Try self.name

validates_uniqueness_of :name, :message => "#{self.name} has already been taken" # also tried using #{:name}

Also validate_uniqueness_of is wrong it should be validates_uniqueness_of

If this not works use validate method and comment line validates_uniqueness_of

 def validate
    name= User.find_by_name(self.name) #Assuming User is your Model Name
    unless name.blank?
      self.errors.add :base, "#{self.name} has already been taken"
    end
 end
Salil
I tried your first solution (`self.name`), but it merely echoed the class' name. I know I could achieve what I want with validate, but I'm trying to see if this is possible using the original method.
sa125