Search code examples
ruby-on-railsdevise

Different Devise configurations for each model


I have two models which are using Devise for authentication: User and Uploader with strictly separated pages and views (the former has access to the app, the latter just to one upload view).

I followed this guide and everything is working well, but now I'm looking for ways to have a different Devise config for each model.

For example: config.authentication_keys = [:email] for User and config.authentication_keys = [:username] for Uploader. Is this possible?


Solution

  • Use the standard Devise config file to set a sensible set of defaults for all configuration options.

    Then for the special cases you want to override in the other model, you can just set them in the model itself:

    class Uploader < ActiveRecord::Base
      devise :database_authenticatable, :registerable, :recoverable, :etc
    
      # Override authentication_keys for Uploader
      self.authentication_keys = [:username]
    end
    

    Should do the trick.