socialforge/db/migrate/20161015102324_update_all_r...

24 lines
904 B
Ruby

class UpdateAllRake < ActiveRecord::Migration
def up
attachments = Attachment.where("is_publish = 0 and publish_time <= '#{Date.today}'")
attachments.update_all(:is_publish => 1)
exercises = Exercise.where("publish_time is not null and exercise_status = 1 and publish_time <=?",Time.now)
exercises.update_all(:exercise_status => 2)
end_exercises = Exercise.where("end_time <=? and exercise_status = 2",Time.now)
end_exercises.each do |exercise|
course = exercise.course
exercise.update_column('exercise_status', 3)
course.student.each do |student|
if ExerciseUser.where("user_id = ? && exercise_id = ?",student.student_id,exercise.id).empty?
ExerciseUser.create(:user_id => student.student_id, :exercise_id => exercise.id, :start_at => exercise.end_time, :status => true,:score=>0)
end
end
end
end
def down
end
end