简体   繁体   English

PaperClip:一个用于多个文件上传的数据库条目

[英]PaperClip: one database entry for multiple file uploads

For example, I uploaded two files A and B in the same form, paperclip will insert two entries into the database. 例如,我以相同的形式上传了两个文件A和B,回形针将两个条目插入数据库。

Is there a way to force paperclip to create only one database entry and insert into two different fields (eg file_name_A, file_name_B, file_size_A, file_size_B....)? 有没有一种方法可以强制回形针仅创建一个数据库条目并将其插入两个不同的字段(例如,file_name_A,file_name_B,file_size_A,file_size_B ....)?

UPDATE : 更新

in my submission.rb: 在我的submission.rb中:

attr_accessible :id, :email, :uploads_attributes
has_many :uploads, :dependent => :destroy
accepts_nested_attributes_for :uploads, :allow_destroy => true

in my upload.rb : 在我的upload.rb中

belongs_to :submission
attr_accessible :id, :user_id, :package_a_file_name, :package_a_file_size, :package_b_file_name, :package_b_file_size, :updated_at
has_attached_file :package

There's nothing wrong with the way your model is set up. 您的模型建立方式没有任何问题。 Storing multiple uploads in a separate model makes it easier to maintain down the track, should you ever wish to change the requirements. 如果您希望更改要求,则将多个上载存储在一个单独的模型中可以更轻松地保持正常运行。

You've only specified one attached file in upload.rb, which is why it inserts a separate entry for each upload. 您仅在upload.rb中指定了一个附件文件,这就是为什么它为每个上载插入一个单独的条目的原因。 Specifying random attr_accessibles won't do anything, those fields don't even exist. 指定随机的attr_accessibles不会做任何事情,这些字段甚至不存在。

You could remove the upload model altogether, and just store the uploads directly on the submission: 可以完全删除上传模型,而直接将上传内容存储在提交中:

submission.rb Submit.rb

has_attached_file :package_a
has_attached_file :packabe_b

This would store the uploads in a single row, associated to the submission. 这会将上传内容存储在与提交关联的单个行中。 This is not scalable. 这是不可扩展的。

I would not store multiple uploads in your upload model in an unscalable fashion as above, that wouldn't make logical sense from an OOP point of view. 我不会像上面那样以不可缩放的方式在您的上传模型中存储多个上传,从OOP的角度来看,这在逻辑上是没有道理的。

All in all, I think the way you've got it set up now is the best approach. 总而言之,我认为您现在设置它的方法是最好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM