You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/lib/tasks/course_board_message_count....

43 lines
1.4 KiB

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)
6 years ago
if root_subquery.present?
children_subquery = Message.where(parent_id: root_subquery).reorder(nil).pluck(:id)
if children_subquery.present?
second_children_subquery = Message.where(parent_id: children_subquery).reorder(nil).pluck(:id)
6 years ago
ids = children_subquery.concat(second_children_subquery).uniq
Message.where(id: ids).update_all(board_id: board.id)
end
end
Board.reset_counters(board.id, :messages)
logger("transfer success ~")
end
end
desc 'transfer root id && statistic descendants count'
task messages_descendants_count_transfer: :environment do
Message.root_nodes.find_each do |root|
logger("Current transfer root id: #{root.id} ~")
children_ids = root.children.pluck(:id)
next if children_ids.blank?
second_children_ids = Message.where(parent_id: children_ids).pluck(:id)
ids = children_ids.concat(second_children_ids).uniq
Message.where(id: ids).update_all(root_id: root.id)
Message.reset_counters(root.id, :descendants)
logger("transfer success ~")
end
end
def logger(msg)
puts msg
end
end