delete belongs_to instance if no instances of has_many exist

There are a few options. Mostly it comes down to when you want to do this.

I’d suggest you do it when deleting a DocumentsFoldersRequest

Any time you delete one, check the parent folder and child_folder:

class DocumentsFoldersRequest < ApplicationRecord
  after_destroy :check_for_orphaned_folder

  def check_for_orphaned_folder
    folder.destroy if folder.documents_folders_requests.none?
    child_folder.destroy if child_folder.documents_folders_requests.none?
  end

end

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top