class AddPraisesCountToMessages < ActiveRecord::Migration[5.2] def change add_column :messages, :praises_count, :integer, :default => 0 Message.find_each do |msg| total_count = msg.praise_treads.liker.count puts "====> set message's praises_count to #{total_count}" msg.update_column(:praises_count, total_count) end end end