class UserGrade < ActiveRecord::Base
  # attr_accessible :title, :body
  attr_accessible :user_id, :project_id, :grade
  belongs_to :project
  before_save :correct_score
  validates_uniqueness_of :user_id, :scope => [:project_id]
  #validate :my_validation

  #修正分数
  #分数小于0时修正为0
  #分数大于等于0时不修正  -by zjc
  def correct_score
    if !self.grade.nil? && self.grade < 0
      self.grade = 0
    end
  end

  #def self.exit_user_and_project(user,project)
  #  unless user.nil? || project.nil?
  #    if user.class.to_s == 'User' && project.class.to_s == 'Project'
  #      user_grade = UserGrade.find_by_user_id_and_project_id(user.id,project.id)
  #      user_grade
  #    else
  #      nil
  #    end
  #  else
  #    nil
  #  end
  #end

  #def my_validation
  #  if user_id.nil? || project_id.nil?
  #    errors[:Base] << "用户和项目不能为空"
  #  elsif UserGrade.exit_user_and_project(user_id,project_id)
  #    errors[:Base] << "当前记录已存在"
  #  end
  #end
end