Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single File Delete #2287

Closed
hacktorious opened this issue Mar 1, 2018 · 2 comments
Closed

Single File Delete #2287

hacktorious opened this issue Mar 1, 2018 · 2 comments

Comments

@hacktorious
Copy link

hacktorious commented Mar 1, 2018

I've spent the entire day trying to figure out how to successfully delete individual files, which were uploaded and stored as an array/Json in MySQL. I'm completely puzzled and need some help.

I have been unable to find a solution on the net that works.

I upload a list of files to an album, say 150 images. Then I want to delete something like 5 images. How can I do that? I don't want to delete the entire list.

I would expect to be able to call something like album.remove_images([1.jpg,2.jpg,3.jpg]), or even use indexes. Using indexes doesn't work because if you iterate over the list as you delete items the index count changes.

Doing something like the following only allows the user to delete a single file at a time, which would require multiple hits to the database and be a serious headache of they want to remove like 35 of the 150 files.

def remove_image_at_index(index)
  remain_images = @gallery.images # copy the array
  deleted_image = remain_images.delete_at(index) # delete the target image
  deleted_image.try(:remove!) # delete image from S3
  @gallery.images = remain_images # re-assign back
end

I'm totally baffled, please help. Thanks.

@btrewern
Copy link

Why can't you do something like this:

def remove_image_at_index(indexes)
  images = @gallery.images # copy the array
  remain_images = []
  images.each_with_index do |image, index|
    if indexes.include?(index)
      deleted_image = images[index]
      deleted_image.try(:remove!) # delete image from S3
    else
      remain_images << images[index]
    end
  end
  @gallery.images = remain_images
end

@mshibuya
Copy link
Member

Closed by #2401.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants