diff --git a/lib/tasks/fill_in_content_id_in_list_items.rake b/lib/tasks/fill_in_content_id_in_list_items.rake new file mode 100644 index 000000000..7ac0fbe60 --- /dev/null +++ b/lib/tasks/fill_in_content_id_in_list_items.rake @@ -0,0 +1,15 @@ +desc "Fill in content ids in list items after adding content_id database column." +task fill_in_content_id_in_list_items: :environment do + ListItem.find_each do |list_item| + next if list_item.read_attribute(:content_id).present? + + base_path = list_item.base_path + list_item.content_id = list_item.list.tag.tagged_document_for_base_path(base_path)&.content_id + + unless list_item.read_attribute(:content_id).present? && list_item.save + puts "Could not set content_id of the following list item: #{list_item.id} #{list_item.base_path} #{list_item.title}" + end + rescue StandardError => e + puts "Exception #{e} raised when trying to set content_id of the following list item: #{list_item.id} #{list_item.base_path} #{list_item.title}" + end +end