27

Now I have one devise model, which uses email as authentication_key.

I want to add one new devise model, which uses student_id as authentication_key.

Some guide tells me to modify the configuration

"config.authentication_keys = [ :email ]" by replacing the :email with :student_id.

After modification, the first model login always fails, so I think I have to indicate different authentication_keys for the two models separately.

How should I do?

Roy
  • 271
  • 3
  • 3

1 Answers1

88

You will have to declare inside your models which are the authenitcation keys, rather than inside the devise.rb file.

class model1 < ActiveRecord::Base

devise :database_authenticatable, :rememberable, :trackable, :authentication_keys => [:email]

and for your second model

class model2 < ActiveRecord::Base

devise :database_authenticatable, :rememberable, :trackable, :authentication_keys => [:studentid]

also make sure that you comment out from devise.rb the config.authentication_keys settings

Dimitris
  • 2,501
  • 3
  • 21
  • 28
  • 2
    Roy, if the answer is suitable then please feel free to mark it as a correct answer (I am trying to build some reputation!). Many thanks! – Dimitris Jan 08 '11 at 08:49
  • This just saved my arse while implementing an app which sits atop Refinery CMS which uses Devise for its own authentication. Thanks! – Paul Russell Mar 10 '11 at 23:09
  • yeah dude give him some credit and mark it as correct answer! ;) (worked for me too...) – fluxsaas Sep 21 '11 at 10:30
  • 3
    Important: If you're using Recoverable, make sure you add ````:reset_password_keys => [:email]````, otherwise the password resets will always be scoped on :email (!) – wspruijt Apr 25 '14 at 13:35