class ModifyViewedCountForSubjects < ActiveRecord::Migration[5.2] def change subjects = Subject.where(status: 2).includes(:shixuns) subjects.find_each do |subject| subject.update_attribute(:visits, subject.visits + subject.shixuns.pluck(:myshixuns_count).sum) end end end