guoren/app/models/user.rb

30 lines
988 B
Ruby
Raw Normal View History

2016-12-22 20:00:08 +08:00
class User < ApplicationRecord
2016-12-22 20:05:38 +08:00
before_save :default_values
def default_values
2017-01-14 15:43:34 +08:00
if self.picurl.nil?
self.picurl = 'images/avatars/default/avatar.png'
end
2016-12-22 20:05:38 +08:00
end
2016-12-27 17:45:57 +08:00
has_many :send_messages, class_name: 'Message', foreign_key: 'send_user', dependent: :destroy
has_many :recieve_messages, class_name: 'Message', foreign_key: 'recieve_user', dependent: :destroy
2016-12-22 20:05:38 +08:00
has_many :micro_posts, dependent: :destroy
has_many :comments, class_name: 'Comment', foreign_key: 'user_id', dependent: :destroy
2016-12-22 20:00:08 +08:00
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
2017-01-06 21:09:03 +08:00
2016-12-22 20:00:08 +08:00
end