2015-11-13 14:40:14 +08:00
|
|
|
|
class Exercise < ActiveRecord::Base
|
2015-11-13 17:23:52 +08:00
|
|
|
|
#exercise_status: 1,新建;2,发布;3,关闭
|
2015-11-13 14:40:14 +08:00
|
|
|
|
include Redmine::SafeAttributes
|
|
|
|
|
belongs_to :user
|
2015-11-30 12:55:19 +08:00
|
|
|
|
belongs_to :course ,:touch => true
|
2015-11-13 14:40:14 +08:00
|
|
|
|
has_many :exercise_questions, :dependent => :destroy,:order => "#{ExerciseQuestion.table_name}.question_number"
|
|
|
|
|
has_many :exercise_users, :dependent => :destroy
|
2015-11-13 15:47:39 +08:00
|
|
|
|
has_many :users, :through => :exercise_users #该测试被哪些用户提交答案过
|
2015-12-04 10:56:16 +08:00
|
|
|
|
# 课程消息
|
|
|
|
|
has_many :course_messages, :class_name =>'CourseMessage', :as => :course_message, :dependent => :destroy
|
|
|
|
|
after_create :acts_as_course_message
|
|
|
|
|
def acts_as_course_message
|
|
|
|
|
if self.course
|
|
|
|
|
if self.exercise_status == 2 #未发布
|
|
|
|
|
#self.course.members.each do |m|
|
|
|
|
|
self.course_messages << CourseMessage.create(:user_id => User.current.id, :course_id => self.course_id, :viewed => false,:status=>2)
|
|
|
|
|
#end
|
|
|
|
|
# else
|
|
|
|
|
# self.course_messages.destroy_all 这里的destory_all值得商榷。因为我这里是通过status来控制不同的status的
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-11-13 14:40:14 +08:00
|
|
|
|
end
|