require 'bcrypt' class User < ApplicationRecord # users.password_hash in the database is a :string include BCrypt def password return nil if password_hash.nil? @password ||= Password.new(password_hash) end def password=(new_password) @password = Password.create(new_password) self.password_hash = @password end def match?(password) self.password == password end end