rails-activestorageruby-on-rails-7

Rails 7 Active Storage - how do I upload/add image(s) to list of existing images without overriding/purging existing images?


I have got a Listing model to which I upload multiple images. I just can't figure out how to upload additional images to each Listing without purging/replacing existing images.

Here's the scenario: I upload one or more images, upon editing the files, when I attempt to upload new images, the existing ones are simply replaced by the new one(s). How do I keep both the existing and the new ones?

I have successfully added a check to delete feature in my update method (see below), and all I need is to be able to add newly uploaded images to whatever is left of the existing images.

Class Listing
has_many_attachments :photos

Listing Controller

def update
  if params[:listing][:photo_ids]
      params[:listing][:photo_ids].each do |photo_id|
        photo = listing.photos.find(photo_id)
        photo.purge
      end
    end

Solution

  • The solution on Rail 6 is to add the following line to configuration in config/application.rb

    config.active_storage.replace_on_assign_to_many = false
    

    source: Details here