We currently switched to Rails 5.2 to get the awesome functionalities of ActiveStorage.
Up until now I was able to upload, delete (purge), and show files as expected - but I am not quite sure on how to update an attachment the right way.
Here are the steps to reproduce:
user.avatar.attached?
=> false
user.avatar.attach(io: File.open("~/avatar.png"), content_type: "image/png", filename: "avatar")
=> #<ActiveStorage::Attachment id: 2, name: "avatar", record_type: "User", ...
user.avatar.attached?
=> true
user.avatar.attach(io: File.open("~/avatar2.png"), content_type: "image/png", filename: "avatar2")
=> Exception: ActiveRecord::RecordNotSaved: Failed to remove the existing associated avatar_attachment. The record failed to save after its foreign key was set to nil.
user.avatar.purge
=> nil
user.avatar.attached?
=> false
user.avatar.attach(io: File.open("~/avatar2.png"), content_type: "image/png", filename: "avatar2")
=> #<ActiveStorage::Attachment id: 2, name: "avatar2", record_type: "User", ...
I don't think it should be necessary to purge the first attachment to attach a new one. So how can this be achieved properly?