Ruby on Rails Friday, August 25, 2017

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?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/54fc80a5-f136-44e8-a59d-112e15607cf4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment