Rails - 使用 Paperclip 临时存储文件,然后移动到另一个模型 [英] Rails - Using Paperclip to temporarily store files and then move to another model

查看:33
本文介绍了Rails - 使用 Paperclip 临时存储文件,然后移动到另一个模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这怎么可能?

当用户上传文件时,我想使用回形针将文件上传到 AttachmentTemp 模型,并在 S3 上使用某种类型的临时文件夹.

When a user upload files, I want to upload the file to an AttachmentTemp model using paperclip, and use some type of temp folder on S3.

然后在我的延迟作业中,使用该临时文件,但将该文件保存到附件模型回形针中,在那里可以通过 delay_job 进行处理.

Then in my delayed job, use that temp file, but save that file to the Attachment model paperclip where it can be processed via delayed_job.

可能吗?

推荐答案

我还没试过,但是下面的怎么样?

I haven't tried it, but how about the following?

首先,为两个模型运行 Paperclip 生成器,以便两个表在数据库中都有必要的列(迁移后).

First, run the Paperclip generator for both models, so both tables have the necessary columns in the database (post migration).

然后在面向用户的新操作/视图中使用 Temp 模型.

Then use the Temp model in your user-facing new action/view.

稍后,在您的延迟作业中,加载每个 AttachmentTemp 实例(根据某些状态标准过滤)并将每个实例复制到一个 Attachment 实例.所以你只是将一个翻译成另一个.

Later, in your delayed job, load up each AttachmentTemp instance (filtered on some status criterion) and copy each one to an Attachment instance. So you're just translating one to the other.

在剩余的面向用户的操作中,使用 Attachment 实例(因此 AttachmentTemps 仅在首次创建记录时使用).

In your remaining user-facing actions, use Attachment instances (so AttachmentTemps are only used when first creating a record).

这篇关于Rails - 使用 Paperclip 临时存储文件,然后移动到另一个模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆