class StudentsForCourse < ActiveRecord::Base attr_accessible :course_id, :student_id belongs_to :course, :class_name => 'Course', :foreign_key => :course_id belongs_to :student, :class_name => 'User', :foreign_key => :student_id validates_presence_of :course_id, :student_id validates_uniqueness_of :student_id, :scope => :course_id after_destroy :delete_student_works def delete_student_works course = self.course homework_ids = course.homework_commons.blank? ? "(-1)" : "(" + course.homework_commons.map{|hw| hw.id}.join(",") + ")" student_works = StudentWork.where("user_id = #{self.student_id} && homework_common_id in #{homework_ids}") student_works.destroy_all end end