ambition/app/models/comment.rb

44 lines
1.1 KiB
Ruby
Raw Normal View History

2017-01-02 10:57:57 +08:00
class Comment < ActiveRecord::Base
2016-12-26 12:13:59 +08:00
belongs_to :mission
2017-01-02 10:57:57 +08:00
2016-12-27 20:15:05 +08:00
validates :content, presence: true
validates :mission_id, presence: true
2016-12-26 12:13:59 +08:00
2016-12-29 17:33:25 +08:00
validates :user_id, presence: true
2017-01-02 10:57:57 +08:00
validate :comment_has_atleast_one_character, on: :create
2016-12-29 17:33:25 +08:00
validate :user_id_exist, on: :create
2016-12-27 20:15:05 +08:00
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
2016-12-29 17:33:25 +08:00
def user_id_exist
user = User.find_by(id: user_id)
if user == nil
errors[:user] = "user is not exist"
end
end
2016-12-27 20:15:05 +08:00
# 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
2016-12-26 12:13:59 +08:00
end