educoder/app/models/myshixun.rb

39 lines
1.1 KiB
Ruby

class Myshixun < ActiveRecord::Base
attr_accessible :description, :name, :parent_id, :user_id, :gpid, :forked_from, :visits, :is_public, :identifier
has_many :users, :through => :myshixun_members
has_many :myshixun_members
has_one :repository
has_many :games, :dependent => :destroy, :order => "games.id ASC"
belongs_to :shixun
# id 转换成 identifier
def to_param
identifier
end
# 当前任务:一个实训中只可能一个未完成任务(status 0或1只会存在一条记录)
# status:0 可以测评的,正在测评的
# 如果都完成,则当前任务为最后一个任务
def current_task
games = self.games
current_game = games.select{|game| game.status==1 || game.status==0}.first
if current_game.blank?
passed_games = games.select{|game| game.status==2}
current_game = passed_games.last
end
return current_game
end
def parent
Shixun.find(self.parent_id)
rescue ActiveRecord::RecordNotFound
render_404
end
def owner
User.find(self.user_id)
rescue ActiveRecord::RecordNotFound
render_404
end
end