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? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You use this :) validates :email, :uniqueness => true, :allow_blank => true RobL http://www.robl.me On 15 October 2010 16:33, Leonel *.* <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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? > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Rob Lacey contact-+WAvBcCRUVA@public.gmane.org http://www.robl.me -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Oct 15, 2010, at 11:33 AM, Leonel *.* wrote:> 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?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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Awesuuum! Thanks! Sorry, still a newbie :P -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.