"has already been taken" on empty field

I have a form to add clients and enter their email. The email is not
required but should be unique, so I have this at the model.

validates :email, :uniqueness => true

Some clients have NO email on record. So when I try to add a second
client with no email, I get this validation message: "Email has already
been taken". I supposed because this happens because sees a current
empty email row and then I'm submitting an empty email field so it they
are not unique anymore because they are both empty.

How can I allow for empty email fields but also check for uniqueness?

You use this :slight_smile:

validates :email, :uniqueness => true, :allow_blank => true


Try this:

validates :email, :uniqueness => true, :unless parameters[:user][:email].blank?

Not sure about that last part, but do you see what I'm trying for there? You might need to make a controller-level function to check if the current item has an empty e-mail property.


Awesuuum! Thanks! Sorry, still a newbie :stuck_out_tongue: