#encoding: utf-8 class AddStudentWorkData < ActiveRecord::Migration def up count = Course.all.count / 30 + 2 transaction do for i in 1 .. count do i Course.page(i).per(30).each do |course| begin course.student.each do |student| course.homework_commons.each do |hw| if hw.homework_type != 3 && hw.student_works.where("user_id = #{student.student_id}").count == 0 hw.student_works << StudentWork.new(:user_id => student.student_id, :name => hw.name.to_s+"的作品提交", :work_status => 0) end end end rescue Exception => e logger.error "[AddStudentWorkData] ===> #{e}" end end end end end def down end end