class AddVideoToCourseModule < ActiveRecord::Migration[5.2] def change Course.all.each do |course| unless course.course_modules.exists?(module_type: "video") atta_position = course.course_modules.find_by(module_type: 'attachment')&.position.to_i video_position = atta_position != 0 ? (atta_position + 1) : 11 course.course_modules.where("position >= #{video_position}").update_all("position = position + 1") course.course_modules << CourseModule.new(module_type: "video", hidden: 1, module_name: "视频", position: video_position) end end end end