class Competition < ApplicationRecord has_many :competition_modules, dependent: :destroy has_many :competition_stages, dependent: :destroy has_many :competition_stage_sections, dependent: :destroy has_one :current_stage_section, -> { where('end_time > NOW()') }, class_name: 'CompetitionStageSection' has_many :team_members, dependent: :destroy has_many :competition_staffs, dependent: :destroy has_one :teacher_staff, -> { where(category: :teacher) }, class_name: 'CompetitionStaff' has_one :member_staff, -> { where.not(category: :teacher) }, class_name: 'CompetitionStaff' has_many :attachments, as: :container after_create :create_competition_modules # 是否上架 def published? status? end # 报名是否结束 def enroll_ended? enroll_end_time.blank? || enroll_end_time < Time.now end # 是否已经报名 def enrolled?(user) team_members.exists?(user_id: user.id) end private def create_competition_modules CompetitionModule.bulk_insert(*%i[competition_id name position]) do |worker| %w(首页 报名 通知公告 排行榜 资料下载).each_with_index do |name, index| worker.add(competition_id: id, name: name, position: index + 1) end end end end