class ClassStudent < Base::ClassStudent
  audited

  belongs_to :classroom, primary_key: 'ClassID', foreign_key: 'ClassID'
  belongs_to :student, primary_key: 'StudentID', foreign_key: 'StudentID'

  has_many :documents, through: :classroom
  has_many :news_articles, through: :classroom
  has_many :school_year_students, through: :student

  scope :by_students, ->(students) { where(student_id: students) if students.present? }
  scope :by_classroom, ->(classroom) { where(class_id: classroom) if classroom }
  scope :by_classroom_type, ->(type) { joins(:classroom).merge(Classroom.by_type(type)) if type }
  scope :with_user, -> { joins(:student).merge(Student.with_user) }
end
