1

I have a Rails 6 app.

User has_one Shop

Shop has_one_attached Logo

User.update with nested_attributes purges the User.shop.company_logo.

class User
  has_one :shop, dependent: :destroy # delete Shop if user gets deleted 
  accepts_nested_attributes_for :shop, # also tried reject_if: :all_blank
end

class Shop
  belongs_to :user
  has_one_attached :company_logo do |attachable|
    attachable.variant :thumbnail, resize_to_fill: [100, 100]
  end
end


# creating a User
User.create({name: "Test"})

# creating a Shop
logo = File.open(Rails.root.join('spec', 'fixtures', 'files', '400x400.png'))
Shop.create(user_id: User.last.id, name: "TestShop", company_logo: logo)
User.last.shop.company_logo.attached? # returns TRUE !!!

# Update User (deletes / detaches company_logo) 
params = {name: "TestUpdateName", shop: {name: "TestUpdateShop"}}
User.last.update(params)
User.last.shop.company_logo.attached? # returns FALSE !!!

Console output:

[ActiveJob] Enqueued ActiveStorage::PurgeJob (Job ID: caebfa82-7c41-4eb9-b295-c213449e0a7e) to Async(active_storage_purge) with arguments: #<GlobalID:0x00007fcf6ae179e0 @uri=#<URI::GID gid://meta-shop/ActiveStorage::Blob/81>>
ActiveStorage::Blob Load (1.7ms)  SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 81], ["LIMIT", 1]]
[ActiveJob] [ActiveStorage::PurgeJob] [caebfa82-7c41-4eb9-b295-c213449e0a7e] Performing ActiveStorage::PurgeJob (Job ID: caebfa82-7c41-4eb9-b295-c213449e0a7e) from Async(active_storage_purge) enqueued at 2021-09-25T10:01:32Z with arguments: #<GlobalID:0x00007fcf5a8856b8 @uri=#<URI::GID gid://meta-shop/ActiveStorage::Blob/81>>
[ActiveJob] [ActiveStorage::PurgeJob] [caebfa82-7c41-4eb9-b295-c213449e0a7e]   TRANSACTION (2.0ms)  BEGIN
[ActiveJob] [ActiveStorage::PurgeJob] [caebfa82-7c41-4eb9-b295-c213449e0a7e]   ActiveStorage::Attachment Exists? (2.1ms)  SELECT 1 AS one FROM "active_storage_attachments" WHERE "active_storage_attachments"."blob_id" = $1 LIMIT $2  [["blob_id", 81], ["LIMIT", 1]]
[ActiveJob] [ActiveStorage::PurgeJob] [caebfa82-7c41-4eb9-b295-c213449e0a7e]   ActiveStorage::Attachment Load (2.8ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 81], ["record_type", "ActiveStorage::Blob"], ["name", "preview_image"], ["LIMIT", 1]]
[ActiveJob] [ActiveStorage::PurgeJob] [caebfa82-7c41-4eb9-b295-c213449e0a7e]   ActiveStorage::Blob Destroy (8.8ms)  DELETE FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1  [["id", 81]]
[ActiveJob] [ActiveStorage::PurgeJob] [caebfa82-7c41-4eb9-b295-c213449e0a7e]   TRANSACTION (5.1ms)  COMMIT
[ActiveJob] [ActiveStorage::PurgeJob] [caebfa82-7c41-4eb9-b295-c213449e0a7e]   S3 Storage (224.0ms) Deleted file from key: mx5g91bi8qwgvh6a8vzh65598kd3
[ActiveJob] [ActiveStorage::PurgeJob] [caebfa82-7c41-4eb9-b295-c213449e0a7e]   S3 Storage (311.1ms) Deleted files by key prefix: variants/mx5g91bi8qwgvh6a8vzh65598kd3/
[ActiveJob] [ActiveStorage::PurgeJob] [caebfa82-7c41-4eb9-b295-c213449e0a7e] Performed ActiveStorage::PurgeJob (Job ID: caebfa82-7c41-4eb9-b295-c213449e0a7e) from Async(active_storage_purge) in 568.39ms

How can I persist the User.shop.company_logo when updating with nested_attributes ?

Jan
  • 12,992
  • 9
  • 53
  • 89
  • Well, you did update the User and gave new params for Shop. This being something like a PUT request (and not PATCH) removed the attachment. I feel this is a wrong approach in itself. Happy for someone to provide an alternate solution. – manu29.d Sep 25 '21 at 17:10
  • My goal is to not replace the Shop, it should only supplement (or edit the given nested fields). – Jan Sep 25 '21 at 17:15

1 Answers1

1

I found a solution:

My Shop model was replaced when updating through the parent User model. But there is an option which can be added on "one-to-one" relations like has_one. It's the update_only option, which is false by default. When using this, the nested model get updated and not replaced when using update_only: false

accepts_nested_attributes_for :shop, update_only: true

https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for

Jan
  • 12,992
  • 9
  • 53
  • 89