On Rails 6 when new files are being uploaded and at the same time files are marked for destruction, those marked for destruction do not get destroyed. On Rails 6 files marked for destruction get deleted from active storage attachments, but then immediately get reinserted again, thus not being destroyed. It worked correctly for Rails 5, but I can't get it to work for Rails 6.
class Post < ApplicationRecord
has_many_attached :images
accepts_nested_attributes_for :images_attachments, allow_destroy: true
end
class PostsController < ApplicationController
.
.
.
private
def set_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :body, images: [], images_attachments_attributes: [:id, :_destroy])
end
end
On application.rb
config.active_storage.replace_on_assign_to_many = false
Here 2 images where marked for destruction and 1 uploaded, 2 where deleted from active storage attachments, but then 3 get inserted. Then ActiveStorage::PurgeJob rollbacks when active storage attachment is not found.
Started PATCH "/posts/2" for ::1 at 2021-04-06 15:45:34 -0400
Processing by PostsController#update as HTML
Parameters: {"authenticity_token"=>"[FILTERED]", "post"=>{"title"=>"", "images"=>[#<ActionDispatch::Http::UploadedFile:0x000000000c840560 @tempfile=#<Tempfile:C:/Users/meagu/AppData/Local/Temp/RackMultipart20210406-3528-109h3t7.jpg>, @original_filename="0022.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post[images][]\"; filename=\"0022.jpg\"\r\nContent-Type: image/jpeg\r\n">], "images_attachments_attributes"=>{"0"=>{"_destroy"=>"1", "id"=>"12"}, "1"=>{"_destroy"=>"1", "id"=>"13"}, "2"=>{"_destroy"=>"0", "id"=>"10"}, "3"=>{"_destroy"=>"0", "id"=>"11"}, "4"=>{"_destroy"=>"0", "id"=>"14"}}}, "commit"=>"Update Post", "id"=>"2"}
Post Load (0.4ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = 2 LIMIT 1
↳ app/controllers/posts_controller.rb:33:in `set_post'
TRANSACTION (0.3ms) BEGIN
↳ app/controllers/posts_controller.rb:26:in `update'
ActiveStorage::Blob Load (0.9ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" INNER JOIN "active_storage_attachments" ON "active_storage_blobs"."id" = "active_storage_attachments"."blob_id" WHERE "active_storage_attachments"."record_id" = 2 AND "active_storage_attachments"."record_type" = 'Post' AND "active_storage_attachments"."name" = 'images'
↳ app/controllers/posts_controller.rb:26:in `update'
ActiveStorage::Attachment Load (0.6ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = 2 AND "active_storage_attachments"."record_type" = 'Post' AND "active_storage_attachments"."name" = 'images' AND "active_storage_attachments"."id" IN (12, 13, 10, 11, 14)
↳ app/controllers/posts_controller.rb:26:in `update'
ActiveStorage::Attachment Destroy (0.5ms) DELETE FROM "active_storage_attachments" WHERE "active_storage_attachments"."id" = 12
↳ app/controllers/posts_controller.rb:26:in `update'
ActiveStorage::Attachment Destroy (0.4ms) DELETE FROM "active_storage_attachments" WHERE "active_storage_attachments"."id" = 13
↳ app/controllers/posts_controller.rb:26:in `update'
ActiveStorage::Attachment Load (0.5ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = 2 AND "active_storage_attachments"."record_type" = 'Post' AND "active_storage_attachments"."name" = 'images'
↳ app/controllers/posts_controller.rb:26:in `update'
ActiveStorage::Attachment Create (1.2ms) INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ('images', 'Post', 2, 13, '2021-04-06 19:45:35.063794') RETURNING "id"
↳ app/controllers/posts_controller.rb:26:in `update'
ActiveStorage::Attachment Create (0.5ms) INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ('images', 'Post', 2, 14, '2021-04-06 19:45:35.068744') RETURNING "id"
↳ app/controllers/posts_controller.rb:26:in `update'
ActiveStorage::Blob Create (0.5ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "service_name", "byte_size", "checksum", "created_at") VALUES ('crcqufwhuq06cuhz3a52qfo01g2i', '0022.jpg', 'image/jpeg', '{"identified":true}', 'amazon_development', 80275, 'mEm9Vxt3op84oyYTglCISA==', '2021-04-06 19:45:35.077963') RETURNING "id"
↳ app/controllers/posts_controller.rb:26:in `update'
ActiveStorage::Attachment Create (0.4ms) INSERT INTO "active_storage_attachments" ("name", "record_type", "record_id", "blob_id", "created_at") VALUES ('images', 'Post', 2, 18, '2021-04-06 19:45:35.082295') RETURNING "id"
↳ app/controllers/posts_controller.rb:26:in `update'
Post Update (0.5ms) UPDATE "posts" SET "updated_at" = '2021-04-06 19:45:35.084489' WHERE "posts"."id" = 2
↳ app/controllers/posts_controller.rb:26:in `update'
TRANSACTION (2.9ms) COMMIT
↳ app/controllers/posts_controller.rb:26:in `update'
S3 Storage (1063.9ms) Uploaded file to key: crcqufwhuq06cuhz3a52qfo01g2i (checksum: mEm9Vxt3op84oyYTglCISA==)
ActiveStorage::Blob Load (1.6ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = 13 LIMIT 1
↳ app/controllers/posts_controller.rb:26:in `update'
[ActiveJob] Enqueued ActiveStorage::PurgeJob (Job ID: ee6bdc98-5a29-455d-8729-cf1adc68b36b) to Async(default) with arguments: #<GlobalID:0x000000000cc5f830 @uri=#<URI::GID gid://testapprails6/ActiveStorage::Blob/13>>
ActiveStorage::Blob Load (0.8ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = 14 LIMIT 1
↳ app/controllers/posts_controller.rb:26:in `update'
[ActiveJob] Enqueued ActiveStorage::PurgeJob (Job ID: 19fab08a-2756-44bb-814f-45d0ecc85052) to Async(default) with arguments: #<GlobalID:0x000000000ccac9f0 @uri=#<URI::GID gid://testapprails6/ActiveStorage::Blob/14>>
[ActiveJob] Enqueued ActiveStorage::AnalyzeJob (Job ID: 21fca5c1-1c97-465b-b4c7-6fbaf472c2d4) to Async(default) with arguments: #<GlobalID:0x000000000ccbf208 @uri=#<URI::GID gid://testapprails6/ActiveStorage::Blob/18>>
Redirected to http://localhost:3000/posts/2
Completed 302 Found in 1253ms (ActiveRecord: 12.0ms | Allocations: 21838)
Started GET "/posts/2" for ::1 at 2021-04-06 15:45:36 -0400
ActiveStorage::Blob Load (1.4ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = 13 LIMIT 1
[ActiveJob] [ActiveStorage::PurgeJob] [ee6bdc98-5a29-455d-8729-cf1adc68b36b] Performing ActiveStorage::PurgeJob (Job ID: ee6bdc98-5a29-455d-8729-cf1adc68b36b) from Async(default) enqueued at 2021-04-06T19:45:36Z with arguments: #<GlobalID:0x000000000cd80430 @uri=#<URI::GID gid://testapprails6/ActiveStorage::Blob/13>>
[ActiveJob] [ActiveStorage::PurgeJob] [ee6bdc98-5a29-455d-8729-cf1adc68b36b] TRANSACTION (0.3ms) BEGIN
[ActiveJob] [ActiveStorage::PurgeJob] [ee6bdc98-5a29-455d-8729-cf1adc68b36b] ActiveStorage::Attachment Exists? (8.8ms) SELECT 1 AS one FROM "active_storage_attachments" WHERE "active_storage_attachments"."blob_id" = 13 LIMIT 1
[ActiveJob] [ActiveStorage::PurgeJob] [ee6bdc98-5a29-455d-8729-cf1adc68b36b] TRANSACTION (0.4ms) ROLLBACK
[ActiveJob] [ActiveStorage::PurgeJob] [ee6bdc98-5a29-455d-8729-cf1adc68b36b] Performed ActiveStorage::PurgeJob (Job ID: ee6bdc98-5a29-455d-8729-cf1adc68b36b) from Async(default) in 17.44ms
ActiveStorage::Blob Load (1.2ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = 14 LIMIT 1
[ActiveJob] [ActiveStorage::PurgeJob] [19fab08a-2756-44bb-814f-45d0ecc85052] Performing ActiveStorage::PurgeJob (Job ID: 19fab08a-2756-44bb-814f-45d0ecc85052) from Async(default) enqueued at 2021-04-06T19:45:36Z with arguments: #<GlobalID:0x000000000ce97328 @uri=#<URI::GID gid://testapprails6/ActiveStorage::Blob/14>>
[ActiveJob] [ActiveStorage::PurgeJob] [19fab08a-2756-44bb-814f-45d0ecc85052] TRANSACTION (1.1ms) BEGIN
[ActiveJob] [ActiveStorage::PurgeJob] [19fab08a-2756-44bb-814f-45d0ecc85052] ActiveStorage::Attachment Exists? (0.6ms) SELECT 1 AS one FROM "active_storage_attachments" WHERE "active_storage_attachments"."blob_id" = 14 LIMIT 1
[ActiveJob] [ActiveStorage::PurgeJob] [19fab08a-2756-44bb-814f-45d0ecc85052] TRANSACTION (0.3ms) ROLLBACK
[ActiveJob] [ActiveStorage::PurgeJob] [19fab08a-2756-44bb-814f-45d0ecc85052] Performed ActiveStorage::PurgeJob (Job ID: 19fab08a-2756-44bb-814f-45d0ecc85052) from Async(default) in 14.54ms
ActiveStorage::Blob Load (0.5ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = 18 LIMIT 1
[ActiveJob] [ActiveStorage::AnalyzeJob] [21fca5c1-1c97-465b-b4c7-6fbaf472c2d4] Performing ActiveStorage::AnalyzeJob (Job ID: 21fca5c1-1c97-465b-b4c7-6fbaf472c2d4) from Async(default) enqueued at 2021-04-06T19:45:36Z with arguments: #<GlobalID:0x000000000cf1ed78 @uri=#<URI::GID gid://testapprails6/ActiveStorage::Blob/18>>
This is a test app using rails 6.1.3.1 and ruby 2.6.6.