ActiveStorage::FileNotFoundError (ActiveStorage::FileNotFoundError) 在服务器日志中,但在控制台中不可重现
ActiveStorage::FileNotFoundError (ActiveStorage::FileNotFoundError) in server log, but not reproducable in console
我有以下型号:
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 方法时 returns
ActiveStorage::FileNotFoundError (ActiveStorage::FileNotFoundError)
当我在控制台中 运行 Section.last.transload_files
时,它完全按预期执行。我在这里错过了什么?
经过大约两天的实验,我得出的结论是,虽然创建了 ActiveStorage 记录,但它在任何模型回调中都不可用。官方 AS 文档引用了 after-create-commit
回调 here,但是我无法像包含的示例那样调用 blob.open
或 blob.download
。但是从控制台它可以工作。
我能够通过从 Section 模型调用作业然后从该作业调用传输服务来解决这个问题。
就我而言,我注意到在事务中使用 ActiveStorage 附加附件时无法正常工作。这适用于迁移或回调。
我有以下型号:
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 方法时 returns
ActiveStorage::FileNotFoundError (ActiveStorage::FileNotFoundError)
当我在控制台中 运行 Section.last.transload_files
时,它完全按预期执行。我在这里错过了什么?
经过大约两天的实验,我得出的结论是,虽然创建了 ActiveStorage 记录,但它在任何模型回调中都不可用。官方 AS 文档引用了 after-create-commit
回调 here,但是我无法像包含的示例那样调用 blob.open
或 blob.download
。但是从控制台它可以工作。
我能够通过从 Section 模型调用作业然后从该作业调用传输服务来解决这个问题。
就我而言,我注意到在事务中使用 ActiveStorage 附加附件时无法正常工作。这适用于迁移或回调。