ruby-on-railsrails-activestorageacts-as-paranoid

SystemStackError after destroying a model having ActiveStorage + act_as_paranoid - Rails 5.2


DISCLAIMER: The issue is already logged here but to bring more attention to the large community, I am posting the issue here.

I have super simple setup for a model using active storage with acts_as_paranoid

class FileUpload < ApplicationRecord
  acts_as_paranoid
  has_one_attached :file
end

When the model is destroyed, it goes into infinite loop. Have you ever faced such problem? Do you have any solution?


Solution

  • Use

      has_one_attached :file, dependent: :purge_now
    

    When you destroy any record it will schedule a job to destroy the dependent file for the given record.

    So When the job tries to delete the file rails is not able to find the record as it was already deleted.

    So When we use :purge_now and destroy the record, It will delete the dependent file with the record instead of scheduling the job for deletion.