Skip to content
Dave Strus edited this page Nov 13, 2014 · 1 revision

User model

We will not be using Devise in this app. We're going to roll our own authentication system.

Create a User model, inheriting from ActiveRecord::Base.

Rails, since version 3.1, includes a method called has_secure_password that makes rolling our own authentication easier.

class User < ActiveRecord::Base
  has_secure_password
end

has_secure_password does several interesting things. It adds password and password_confirmation methods to your model, but only stores a bcrypt-encrypted password to the database

It automatically adds validations to check for the presence of password and a matching password_confirmation value—neither of which is saved to the database unencrypted—upon creating a new record. It also adds an authenticate method.

All has_secure_password requires is that your database table have a column called password_digest.

Lab: create_users migration

Generate a create_users migration now. Include the following columns, all strings:

  • username
  • name
  • password_digest

Also include timestamp columns.