class CourseSecondCategory < ApplicationRecord default_scope { order("course_second_categories.position ASC") } belongs_to :course belongs_to :course_module belongs_to :parent, class_name: "CourseSecondCategory", foreign_key: "parent_id", optional: true has_many :homework_commons has_many :children, -> { order(position: :asc ) }, class_name: "CourseSecondCategory", foreign_key: "parent_id", dependent: :destroy scope :first_categories, -> { where(parent_id: 0) } validates :name, length: { maximum: 60, too_long: "不能超过60个字符" } def category_type_str category_type == "graduation" && name == "毕设选题" ? "graduation_topics" : ( category_type == "graduation" && name == "毕设任务" ? "graduation_tasks" : category_type ) end def homework_publish_count homework_commons.select{|homework| homework.publish_time.present? && homework.publish_time <= Time.now}.size end def homework_unpublish_count homework_commons.select{|homework| homework.publish_time.nil? || homework.publish_time > Time.now}.size end end