class AddAnnouncementToCourseModules < ActiveRecord::Migration[5.2] def change Course.all.each do |course| unless course.course_modules.exists?(module_type: "announcement") course.course_modules.where.not(module_type: "activity").update_all("position = position + 1") course.course_modules << CourseModule.new(module_type: "announcement", hidden: 1, module_name: "公告栏", position: 2) end end end end