Skip to content

Instantly share code, notes, and snippets.

@GiancarlosIO
Last active July 28, 2016 03:57
Show Gist options
  • Save GiancarlosIO/674a5af4300ae481dbc112df89425d56 to your computer and use it in GitHub Desktop.
Save GiancarlosIO/674a5af4300ae481dbc112df89425d56 to your computer and use it in GitHub Desktop.
class Token < ApplicationRecord
belongs_to :user
before_create :generate_token
def valid?
self.expires_at > DateTime.now
end
private
def generate_token
loop do
self.token = SecureRandom.hex
break unless Token.where(token: self.token).exists?
end
self.expires_at = 3.month.from_now
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment