class ExerciseBank < ApplicationRecord
  # container_type: Poll 问卷； Exercise试卷
  belongs_to :user
  belongs_to :course_list

  has_many :exercise_bank_questions, :dependent => :destroy
  has_many :polls, dependent: :nullify
  has_many :exercises, dependent: :nullify

  # 随机组卷随机规则
  has_many :exercise_bank_random_settings
  # 随机组卷赋分规则
  has_many :exercise_bank_score_settings
  # def course_list_name
  #   self.course_list ? self.course_list.name : ""
  # end
  #
  scope :myself, -> (user_id){where(user_id: user_id)}
  scope :find_by_container, lambda { |id,type| where(container_id: id,container_type:type)}
  scope :find_by_c_type, lambda { |keywords| where(container_type: keywords) unless keywords.blank?}
  scope :public_exercises, -> {where(is_public: true)}
  scope :exercise_bank_search, lambda { |keywords|
    where("name LIKE ?", "%#{keywords}%") unless keywords.blank?}

  validates :name, length: { maximum: 200, too_long: "不能超过200个字符" }
  validates :description, length: { maximum: 20000, too_long: "不能超过20000个字符" }

end