Skip to content

Instantly share code, notes, and snippets.

@ouranos
Last active August 29, 2015 14:21
Show Gist options
  • Save ouranos/5eb811d2df50198ca2a7 to your computer and use it in GitHub Desktop.
Save ouranos/5eb811d2df50198ca2a7 to your computer and use it in GitHub Desktop.
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :username
t.string :authentication_token
t.string :password_digest
t.timestamps null: false
t.index :authentication_token, unique: true
end
end
end
# app/models/user.rb
class User < ActiveRecord::Base
after_create :generate_authentication_token!
has_secure_password
private
# Generate a session token
def generate_authentication_token!
self.authentication_token = Digest::SHA1.hexdigest("#{Time.now}-#{self.id}-#{self.updated_at}")
self.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment