add course board message count rake

dev_course
p31729568 6 years ago
parent e442c30823
commit bea98cb44a

@ -56,11 +56,10 @@ class Message < ApplicationRecord
from_board = Board.find(messages.first.board_id) from_board = Board.find(messages.first.board_id)
root_ids = messages.map(&:id) root_ids = messages.map(&:id)
children = Message.where(parent_id: root_ids).select(:id).to_a children_ids = Message.where(parent_id: root_ids).pluck(:id)
children_ids = children.map(&:id) second_children_ids = Message.where(parent_id: children_ids).pluck(:id)
second_children = Message.where(parent_id: children_ids).select(:id).to_a
ids = root_ids.concat(children_ids).concat(second_children.map(&:id)).uniq ids = root_ids.concat(children_ids).concat(second_children_ids).uniq
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
Message.where(id: ids, board_id: from_board.id).update_all(board_id: to_board.id) Message.where(id: ids, board_id: from_board.id).update_all(board_id: to_board.id)

@ -0,0 +1,24 @@
namespace :course_board do
desc 'transfer children message to root message board'
task children_message_transfer: :environment do
Board.find_each do |board|
logger("Current transfer board id: #{board.id} ~")
root_subquery = board.messages.where(parent_id: nil).reorder(nil).pluck(:id)
next if root_subquery.blank?
children_subquery = Message.where(parent_id: root_subquery).reorder(nil).pluck(:id)
next if children_subquery.blank?
second_children_subquery = Message.where(parent_id: children_subquery).reorder(nil).pluck(:id)
ids = children_subquery.concat(second_children_subquery).uniq
Message.where(id: ids).update_all(board_id: board.id)
Board.reset_counters(board.id, :messages)
logger("transfer success ~")
end
end
def logger(msg)
puts msg
end
end
Loading…
Cancel
Save