ambition/app/models/share.rb

38 lines
999 B
Ruby
Raw Normal View History

class Share < ActiveRecord::Base
2016-12-26 12:13:59 +08:00
2016-12-27 20:15:05 +08:00
validates :content, presence: true
2016-12-29 17:33:25 +08:00
validates :project_id, presence: true
2016-12-27 20:15:05 +08:00
2016-12-29 17:33:25 +08:00
validates :user_id, presence: true
validate :share_has_atleast_one_character
validate :user_exists,on: :create
validate :project_exists,on: :create
private
2016-12-27 20:15:05 +08:00
def share_has_atleast_one_character
tmp = content.strip
if tmp.length < 1
errors[:content] = "share content should has at least one character"
end
end
2016-12-29 17:33:25 +08:00
def user_exists
user = User.find_by(id: user_id)
if user == nil
errors[:user] = "user belong to Share is not exist"
end
end
def project_exists
project = Project.find_by(id: project_id)
if project == nil
errors[:project] = "user belong to Share is not exist"
end
end
2016-12-27 20:15:05 +08:00
end