I’m using the Devise gem for my sign-up process – I only want the email to be required, so I’m auto-generating a password for every user.
For some reason, my code is creating 2 database entries for a single user. One — with the email address. One — with just the encrypted password.
User.rb
password = Devise.friendly_token
User.create!(:email => @current_user_email, :id => @current_user_id, :password => password, :password_confirmation => password)
before_save { |user| user.email = email.downcase }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
In one of my projects, I took a similar path in not requiring the user to register with a password. It has resulted in some major headaches and UX shenanigans FWIW.
Whatever you’re using for persistence, make sure to set a unique index on the email column. This will cause exceptions, but they’re easily handled.
I’d suggest a class method in your user model like:
That should validate the model without using the before_save callback and raise an exception on non-uniqueness