Search code examples
ruby-on-railsauthenticationdevisemodels

Rails 3.2 Are there any advantages to using a separate model for user accounts and profiles?


I would just like to know if there is any advantage to having a different model to the users and the users profile.

This way I would make storing of user details such as first and last name, address and D.O.B separate from the authentication.

Currently I want to use devise for authentication, but am hoping that if I would need or want to change this in the future, doing this would make it easier.

Also I'm thinking there could be some slight performance advantages in not using one model with a larger number of rows?


Solution

  • If you want to prepare for multi-provider authentication (e.g. with omniauth) it's a good idea to separate authentication and identity in your schema. For instance

    class User < ActiveRecord::Base
      has_many :authorizations
    end
    
    class Authorization < ActiveRecord::Base
      belongs_to :user
    end
    

    Then you'd have separate authentication records for each provider the user uses to sign-in, e.g. your local login, Twitter, Facebook, etc.

    It may also be useful to have an Authority model that has_many :authorizations as a good place to keep data (e.g. icons, api keys) specific to each provider.