Search code examples
ruby-on-railsrails-activestorage

Rails Active Storage set folder to store files


I'm using Active Storage to store files in a Rails 5.2 project. I've got files saving to S3, but they save with random string filenames and directly to the root of the bucket. I don't mind the random filenames (I actually prefer it for my use case) but would like to keep different attachments organized into folders in the bucket.

My model uses has_one_attached :file. I would like to specify to store all these files within a /downloads folder within S3 for example. I can't find any documentation regarding how to set these paths.

Something like has_one_attached :file, folder: '/downloads' would be great if that's possible...


Solution

  • As of now ActiveStorage doesn't support that kind of functionality. Refer to this link. has_one_attached just accepts name and dependent.

    Also in one of the GitHub issues, the maintainer clearly mentioned that they have clearly no idea of implementing something like this.

    The workaround that I can imagine is, uploading the file from the front-end and then write a service that updates key field in active_storage_blob_statement