class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable # before_save :ensure_authentication_token devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable before_save :ensure_authentication_token def ensure_authentication_token if authentication_token.blank? self.authentication_token = generate_authentication_token end end def generate_authentication_token loop do token = Devise.friendly_token break token unless User.where(authentication_token: token).first end end end