class MigrateDisciplinePosition < ActiveRecord::Migration[5.2] def change Discipline.all.each_with_index do |discipline, i| discipline.update_column("position", i + 1) discipline.sub_disciplines.each_with_index do |sub, j| sub.update_column("position", j + 1) sub.tag_disciplines.each_with_index do |tag, k| tag.update_column("position", k + 1) end end end end end