All threads / How are folk handling ownership of an ActiveStorage::Attachment?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How are folk handling ownership of an ActiveStorage::Attachment?

Rick Tessner asked in Rails

Hi all,

With carrierwave etc it was easy enough to associate a User with an uploaded image. Things are a bit different with ActiveStorage::Attachment & Blobs.

For example, suppose I have aPark models that has_many_attached :images. I also have a User model and any user can upload an image. I want folk to be able to delete their own images that they've uploaded but not other peoples images.

I can see two ways of doing this:

  • Create a separate model, say Image that belongs_to :user and has_one_attached :picture and then Park instead of having has_many_attachments, just has_many :images
  • Make use of the fact that ActiveStorage::Blob has many ActiveStorage::Attachment and do the ownership of a blob by having two ActiveStorage::Attachment records: One for the Park and the other for the User.

How are people handling ownership of uploaded blobs? Are there other ways of doing this? Is there a best practice around this? Has Chris done a video on this that I've not yet seen?

Thanks all!

Rick

Add the PutObjectAcl permission, which I did via the IAM Management Console. I've created a PR to add this info to the Active Storage guide.

No migration or new service configuration is actually needed. You just need to make the existing objects public, which you can do from the S3 UI (if you don't have many) or with a script. Then you can continue using the same service with public: true added.

Interesting. I don't use S3.

What would a more generic solution be that would apply equally to other storage options, including just local file storage?

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 47,776+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.