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
.