properly orphan deleted docs when also deleting folders

This commit is contained in:
drusepth 2022-12-16 00:55:01 -08:00
parent 2ab4c34fd7
commit 70fe58b90a
2 changed files with 4 additions and 1 deletions

View File

@ -17,7 +17,8 @@ class FoldersController < ApplicationController
def destroy
# Relocate all documents in this folder to the root "folder"
Document.where(folder_id: @folder.id).update_all(folder_id: nil)
# TODO - I think we can handle this at the model association level with dependent: nullify, but I've never used it
Document.with_deleted.where(folder_id: @folder.id).update_all(folder_id: nil)
# Relocate all child folders in this folder to the root "folder"
Folder.where(parent_folder_id: @folder.id).update_all(parent_folder_id: nil)

View File

@ -1,4 +1,6 @@
class Folder < ApplicationRecord
has_many :documents
belongs_to :parent_folder, optional: true, class_name: Folder.name, foreign_key: :parent_folder_id
belongs_to :user