Reputation: 1424
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 (e.g. 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
Upvotes: 0
Views: 397
Reputation: 19750
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.
Upvotes: 1