1

I have some files that I need to save to the database, and every file should be attached to a instance of an ActiveRecord like so:

pdf_files.each do |pdf_file|
 cipher_file = ...
 payslip = MyhraPayslips::Payslip.new(myhra_users_user_id: user_to_update.id, payslip_date: 
          DateTime.parse(date).strftime("%Y-%m-%d"))
 payslip.payslip.attach(io: File.open(cipher_file), filename: File.basename(pdf_file), 
         content_type: 'application/pdf')
 payslip.save
end

Since I have many files, I want to insert them into the database all at once, and, because I have to call attach method on each record, I can't just do it with bulk insert or use create instead of new.

stvar
  • 6,551
  • 2
  • 13
  • 28
AliEutech
  • 7
  • 1
  • Does this answer your question? [Saving multiple objects in a single call in rails](https://stackoverflow.com/questions/2509320/saving-multiple-objects-in-a-single-call-in-rails) – anothermh Sep 30 '20 at 15:50
  • Well it is actually slower that what i have already, because i have to store the data in some Array which is a loop, and loop a second time to attach each file with the created record which call insert each time. – AliEutech Oct 01 '20 at 12:10

0 Answers0