Devise - how to change setting so that email addresses don't need to be unique

20,744

Solution 1

Look in the config/initializers/devise.rb. You can change the default authentication key, which by default is :email to be anything you want, for example:

config.authentication_keys = [ :username ]

Solution 2

= User Model

def email_required?
  false
end

def email_changed?
  false
end

# For ActiveRecord 5.1+
def will_save_change_to_email?
  false
end

= Migration

rails g migration update_index_on_users
def up
  sql = 'DROP INDEX index_users_on_email'
  sql << ' ON users' if Rails.env == 'production' # Heroku pg
  ActiveRecord::Base.connection.execute(sql)
end

Solution 3

Please find the instructions here

Share:
20,744
Jay
Author by

Jay

More of an entrepreneur than a programmer. In '95 I coded a robust app to get my enterprise out of the blocks. In '05 we paid professionals to rewrite the site in Rails 2.2. When the economy turned in '08 we took a hit so I am coding again. Also, just celebrated my 40th anniversary married to wonderwoman! All you young bucks didn't know I was that old, eh?

Updated on July 09, 2022

Comments

  • Jay
    Jay almost 2 years

    I set up Devise to log in with a username instead of email address because a significant number of our users share the same email address with their spouse. It is a unique market that we serve. But each spouse needs separate accounts to record personality instrument results.

    The database no longer requires a unique email address so it will accept the same email addy for two accounts. However, Devise is still requiring a unique email address for each account. Is there a setting or a work around that i can use to change this?

  • Jay
    Jay over 12 years
    I already did that. Devise is still requiring a unique email address. Thanks for your initiative though.
  • eugen
    eugen over 12 years
    In that case, remove :validatable from the model and you should be fine. Add your own custom validations if needed.
  • malclocke
    malclocke about 12 years
    Unfortunately removing :validatable removes a lot of other useful stuff, e.g. password validation. In recent versions of Devise you can also implement email_required? on your model. This will skip email validation if false is returned, see github.com/plataformatec/devise/pull/545
  • Prem
    Prem over 11 years
    I dont want to remove validatable, because, then we have to write all the password validation. I made both the changes mentioned above (in initializers and email_required? method with false),But it didn't work. Any Ideas?
  • semiomant
    semiomant over 10 years
    what Erik Jacobs writes below validated this approach, since the validation he commented out can be disabled by email_changed?. I slightly prefer what Moin Haidar did here, since you don't need to edit the Devise code this way.
  • Ivo Dancet
    Ivo Dancet about 7 years
    Migration can be: remove_index :users, :email