0

I am using this block of code for validating email address. The format of entered email address validates well, but the problem is with the "uniqueness" part - I currently can enter more identic email addresses to the database - how is that possible?

Has something changed in Rails 4 about validations?

class BetaAccess < ActiveRecord::Base
  validates_format_of :email,:with => Devise::email_regexp, uniqueness: true
end

Thank you.

user984621
  • 46,344
  • 73
  • 224
  • 412

3 Answers3

4

Try this:

class BetaAccess < ActiveRecord::Base
  validates :email,format: {with: Devise::email_regexp}, uniqueness: true
end
manu29.d
  • 1,538
  • 1
  • 11
  • 15
  • It should be noted this takes advantage of the [`validates`](http://guides.rubyonrails.org/active_record_validations.html#validations-overview) method introduced in Rails 4 (I think?). Don't use `validates_x_y` any more ;) – Richard Peck Jun 09 '14 at 08:25
  • valides was introduced in Rails 3 – cristian Jun 09 '14 at 09:25
1

format and uniqueness are different validators, if you want to use in one line, you should use validates method.

validates :email, :format => { :with => Devise::email_regexp }, :uniqueness => true
xdazz
  • 158,678
  • 38
  • 247
  • 274
0
validates_format_of :email,:with => Devise::email_regexp, uniqueness: true

combines uniqueness into the validation for format. Use the validates syntax

validates :email,format: {with: Devise::email_regexp},
                 uniqueness: true

Also, use the new ruby syntax for hashes. Kind of neat that way

Hanmaslah
  • 736
  • 1
  • 8
  • 14