class MigrateCourseGroupPosition < ActiveRecord::Migration[5.2] def change Course.all.each do |course| if course.course_groups.exists?(position: 0) course.course_groups.each_with_index do |group, index| group.update_attributes(position: index+1) end end end end end