44 lines
1.1 KiB
Ruby
44 lines
1.1 KiB
Ruby
class Comment < ActiveRecord::Base
|
|
|
|
belongs_to :mission
|
|
|
|
validates :content, presence: true
|
|
validates :mission_id, presence: true
|
|
|
|
validates :user_id, presence: true
|
|
validate :comment_has_atleast_one_character, on: :create
|
|
validate :user_id_exist, on: :create
|
|
|
|
private
|
|
|
|
|
|
def comment_has_atleast_one_character
|
|
tmp = content.strip
|
|
if tmp.length < 1
|
|
errors[:content] = 'content should has at least one character'
|
|
end
|
|
end
|
|
|
|
|
|
def user_id_exist
|
|
|
|
user = User.find_by(id: user_id)
|
|
if user == nil
|
|
errors[:user] = "user is not exist"
|
|
end
|
|
|
|
end
|
|
|
|
|
|
# mission 是否存在的验证将放在controller中进行
|
|
|
|
# def mission_id_should_be_exist
|
|
|
|
# @mission = Mission.find(:mission_id)
|
|
|
|
# if @mission == nil
|
|
# errors[:mission_id] = "mission is not exist"
|
|
# end
|
|
# end
|
|
|
|
end |