【问题标题】:ActiveStorage::FileNotFoundError (ActiveStorage::FileNotFoundError) in server log, but not reproducable in console服务器日志中的 ActiveStorage::FileNotFoundError (ActiveStorage::FileNotFoundError),但在控制台中不可重现
【发布时间】:2020-04-28 15:34:02
【问题描述】:

我有以下型号:

class Section < ApplicationRecord
  has_many_attached :files
  def to_dir
    [client.to_dir, operation.to_dir, self.name.parameterize].join('/')
  end
  after_save :transload_files

  def transload_files
    TransloadService.sync( self.to_dir, self.files )
  end
end

transload_files 方法是问题所在。这里是转载服务:

class TransloadService

    class << self

        def sync(check_dir, files)
            # First we have to check the transload dir for files that have been deleted on the app
            transloaded_files = Request.list(check_dir)
            cull_list = transloaded_files.reject{ |e| files.map{|t| t.filename }.include? Request.filename(e)}
            if cull_list.count > 0
                Request.trim(cull_list)
                p "#{cull_list.count} files trimed from #{check_dir}."
            end

            # Next we have to upload files which arent present in the transload dir

            send_list = files.reject{ |e| transloaded_files.map{|t| Request.filename(t) }.include? e.filename.to_s }
            if send_list.count > 0
                Request.upload_to(check_dir, send_list)
                p "#{send_list.map{|r| r.filename.to_s}.join(', ')} uploaded to #{check_dir}"
            end
        end

    end

end

这是request.rb中的相关代码

class Request
    class << self
        def upload_to(destination, files)
            files.each do |file|
                send_to = connection.object("#{destination}/#{file.filename}")
                file.open{ |tmp| send_to.upload_file(tmp) }
            end
        end
    end
end

我面临的问题是:当after_save 回调运行它返回的 transload_files 方法时 ActiveStorage::FileNotFoundError (ActiveStorage::FileNotFoundError)

当我在控制台中运行Section.last.transload_files 时,它的性能完全符合预期。我在这里错过了什么?

【问题讨论】:

    标签: ruby-on-rails rails-activestorage


    【解决方案1】:

    经过大约两天的实验,我得出的结论是,虽然创建了 ActiveStorage 记录,但它在任何模型回调中都不可用。官方 AS 文档引用了 after-create-commit 回调 here,但是我无法像包含的示例一样调用 blob.openblob.download。但是,从控制台它可以工作。

    我能够通过从 Section 模型调用作业然后从作业调用 transload 服务来解决此问题。

    【讨论】:

      【解决方案2】:

      就我而言,我注意到在事务中使用 ActiveStorage 附加附件时无法正常工作。这适用于迁移或回调。

      【讨论】:

        猜你喜欢
        • 2020-06-04
        • 1970-01-01
        • 2019-02-08
        • 1970-01-01
        • 2018-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多