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 %>
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With