class MigrateCoureGroupPosition < ActiveRecord::Migration[5.2] def change course_ids = [377, 657, 715, 777, 973, 1093, 1131, 1180, 1309, 1920, 2037, 2346, 2354, 2493, 2752, 2920, 3000, 3141, 3240, 3350, 3351, 3353, 3387, 3533, 3796] courses = Course.where(id: course_ids) courses.each do |course| CourseGroup.where(course_id: course.id).reorder("position ASC, CONVERT(course_groups.name USING gbk) COLLATE gbk_chinese_ci ASC").each_with_index do |group, index| group.update!(position: index+1) end end end end