Skip to content

Instantly share code, notes, and snippets.

@hugodias
Created April 11, 2016 00:46
Show Gist options
  • Select an option

  • Save hugodias/4482bdfffaf354a7f7e87cc1ab101ab4 to your computer and use it in GitHub Desktop.

Select an option

Save hugodias/4482bdfffaf354a7f7e87cc1ab101ab4 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
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
private
def generate_authentication_token
loop do
token = Devise.friendly_token
break token unless User.where(authentication_token: token).first
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment