简体   繁体   中英

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.

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....)?

UPDATE :

in my 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 :

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. Specifying random attr_accessibles won't do anything, those fields don't even exist.

You could remove the upload model altogether, and just store the uploads directly on the submission:

submission.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.

All in all, I think the way you've got it set up now is the best approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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