class AddJournalsForMessagesCountForHmAndGtAndGw < ActiveRecord::Migration[5.2]
  def change
    #add_column :homework_commons, :journals_for_messages_count, :integer, :default => 0
    #add_column :graduation_tasks, :journals_for_messages_count, :integer, :default => 0
    #add_column :graduation_topics, :journals_for_messages_count, :integer, :default => 0

    type = ["GraduationTopic", "GraduationTask", "HomeworkCommon"]
    messages = JournalsForMessage.where(jour_type: type).where("m_parent_id is not null")
    # 迁移所有2级以上的回复
    messages.each do |message|
      while message.parent.try(:m_parent_id).present? do
        puts "parent: #{message.parent.try(:m_parent_id)}"
        message.update_attribute(:m_parent_id, message.parent.m_parent_id)
      end
    end

  end
end
