class ContestingProject < ActiveRecord::Base attr_accessible :contest_id, :description, :project_id, :user_id, :reward belongs_to :contest belongs_to :project belongs_to :user DESCRIPTION_LENGTH_LIMIT = 500 validates :description, length: {maximum:DESCRIPTION_LENGTH_LIMIT } validates :user_id, presence: true validates :contest_id, presence: true, uniqueness: {scope: :project_id} validates :project_id, presence: true validate :validate_user validate :validate_contest validate :validate_project def self.create_contesting(contest_id, project_id, description = nil) self.create(:user_id => User.current.id, :contest_id => contest_id, :project_id => project_id, :description => description) end def update_reward(which) self.update_attribute(:reward,which) end def get_reward self.reward end def cancel_contesting unless self.nil? if User.current.id == self.user_id self.destroy end end end private def validate_user errors.add :user_id, :invalid if user.nil? || !user.active? end def validate_contest errors.add :contest_id, :invalid if contest.nil? end def validate_project errors.add :project_id, :invalid if project.nil? end end