"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.

CLIENT
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

RobL
http://www.robl.me

On 15 October 2010 16:33, Leonel . [email protected] wrote:

are not unique anymore because they are both empty.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


Rob L.
[email protected]
http://www.robl.me

On Oct 15, 2010, at 11:33 AM, Leonel . wrote:

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?

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.

Walter

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs