无论如何我可以强制使用这种方法,ActiveStorage::Attached#attach不排队后台工作?换句话说,我想禁用似乎包含在 ActiveStorage::Attached#attach
中的异步行为。以便该方法与 ActiveJob 同步执行而不是异步执行或类似 Sidekiq
最佳答案
这似乎目前有效:
def sync_attach(record, name, attachable)
blob = ActiveStorage::Blob.create_and_upload!(
io: attachable.open, filename: attachable.original_filename
)
blob.analyze
attached = record.send(name)
attached.purge
attached.attach(blob)
end
# Given
class Thing < ActiveRecord::Base
has_one_attached :image
end
# You can do
thing = Thing.create!
sync_attach(thing, :image, attachable)
如果您使用它,您还应该在方法中包含镜像。我欢迎并寻求任何警告、更正、补充等。这是迄今为止我想出的最好的。
关于ruby-on-rails - 如何强制 ActiveStorage::Attached#attach 同步运行——禁用异步行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61309182/