require 'bcrypt' class User < ActiveRecord::Base has_many :posts has_many :sessions validates_presence_of :username validates_presence_of :password_hash def hash(password) BCrypt::Password.create(password) end def authenticate(password) BCrypt::Password.new(@password) == password end end class CreateUserTable < ActiveRecord::Migration[6.0] def change create_table :users do |t| t.string :name t.string :username, null: false, unique: true, index: true t.string :password_hash, null: false t.timestamps end end end