class AddCourseIdToSwp < ActiveRecord::Migration def change add_column :student_work_projects, :course_id, :integer count = StudentWorkProject.all.count / 30 + 2 transaction do for i in 1 ... count do i StudentWorkProject.page(i).per(30).each do |swp| if swp.homework_common && swp.homework_common.course swp.update_column("course_id", swp.homework_common.course_id) end end end end end end