Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active storage has_many_attached is purging previous uploads

I'm trying to add more files to a has_many_attached, but when I upload a new file the previous file is purged. Uploading multiple files does add multiple files, but they are all purged on the next upload as well. Is this intended behavior? if so, how do I prevent the purging?

log.rb

class Log < ApplicationRecord
  has_many_attached :uploads
end

_form.html.erb

<%= form_for @log, remote: true do |f| %>
  <%= f.file_field :uploads, multiple: true %>
<% end %>
like image 758
crazettett Avatar asked Sep 15 '25 07:09

crazettett


1 Answers

You can prevent overwriting the existing attachments by adding the following line to config/environments/development.rb, config/environments/test.rb and config/environments/production.rb, as kindly indicated by quantavi in this issue: https://github.com/richardvenneman/trestle-active_storage/issues/41

config.active_storage.replace_on_assign_to_many = false

Apparently in Rails 6 the default behavior when uploading files again is to purge the previously uploaded files. You can find a longer thread about it in this Rails issue which Aarthi linked in a comment. The line above changes this setting so that consecutive uploads append files instead of overwriting the old ones.

(As you may have inferred from the link to the issue I ran into the same issue when using the Trestle admin panel with the complementary trestle-active_storage gem, that adds active storage field support.)

like image 175
Julia Jacobs Avatar answered Sep 18 '25 00:09

Julia Jacobs