guoren/app/models/user.rb

18 lines
663 B
Ruby

class User < ApplicationRecord
has_many :send_messages, class_name: 'Message', foreign_key: 'send_user'
has_many :recieve_messages, class_name: 'Message', foreign_key: 'recieve_user'
VALID_EMAIL_REGEX = /\A[\w+\-.]+@([a-z\d\-]+\.)+[a-z]+\z/i
before_save { self.email = email.downcase }
validates :name, presence: true, length: {maximum: 50}, uniqueness: {
case_sensitive: false
}
validates :email, presence: true, length: {maximum: 255},
format: {with: VALID_EMAIL_REGEX}, uniqueness: {
case_sensitive: true
}
has_secure_password
validates :password, presence: true, length: {minimum: 6}, allow_nil: true
end