ambition/app/models/mission.rb

48 lines
1.2 KiB
Ruby

class Mission < ActiveRecord:: Base
has_many :comments
validates :name, presence: true
validates :deadline, presence: true
validates :priority, presence: true
validates :status, presence: true
validates :content, presence: true
validates :project_id, presence: true
validate :priority_is_among_valid_values
validate :status_is_among_valid_values
validate :project_exists, on: :create
private
def priority_is_among_valid_values
if priority != 1 and priority != 2 and priority != 3
errors[:priority] = "priority is not among valid values"
end
end
def status_is_among_valid_values
if status != 1 and status != 2 and status != 3 and status != 4
errors[:status] = "status is not among valid values"
end
end
def project_exists
#puts project_id
project = Project.find_by(id: project_id)
#puts project.inspect
if project == nil
errors[:project_id] = "project_id is not exist"
end
end
end