Search code examples
ruby-on-railsrubyrails-activestorage

Tempfile stops existing in instance variable outside of block


I have this method that is meant to copy a file from active storage to a tempfile stored on an instance variable

def initialize(activity)
    ActiveStorage::Downloader
        .new(activity.original_activity_log_file)
        .download_blob_to_tempfile do |tempfile|
            @activity_file_temp = tempfile
            ap @activity_file_temp.path
        end

    ap @activity_file_temp.path
end

When I run this code I see the output

"/tmp/ActiveStorage-32-20190316-23089-fdwpa5.fit"
nil

Why does the file stop existing on the second print? It feels like a scope issue but I don't think instance variables should be affected like this.


Solution

  • I solved this issue by using the download_blob_to method instead which saves to a tempfile you provide rather than returning one.