Add validations and hooks to User model

This commit is contained in:
Justin Edmund 2020-09-25 10:54:08 -07:00
parent 12a98d4abd
commit cb499a68ac

View file

@ -1,2 +1,36 @@
class User < ApplicationRecord
before_save { self.email = email.downcase }
##### ActiveRecord Validations
validates :username,
presence: true,
length: { minimum: 3, maximum: 26 }
validates :email,
case_sensitive: false,
presence: true,
uniqueness: true,
email: true
validates :password,
length: { minimum: 8 },
presence: true,
on: :create
validates :password,
length: { minimum: 8 },
on: :update,
if: :password_digest_changed?
validates :password_confirmation,
presence: true,
on: :create
validates :password_confirmation,
presence: true,
on: :update,
if: :password_digest_changed?
##### ActiveModel Security
has_secure_password
end