how to set default folder prefix at has_one_attachment & has_many_attachment

I read the previous close issues #32790 , #32894 . v6.1.0 has added an option for assigning key when attach a file.

but this option is not allowed yet.

has_one_attached :avatar, prefix: 'user/avatars'

When this kind of option will be supported?

Hello! Welcome and thanks for moving this conversation over to this list. :bow:

I don’t know for sure, but it seems there was no interest in making this feature in Rails and there have been a few workarounds mentioned in those threads for assigning the key as the prefix.

So we really can’t answer the question “when” yet, that ultimately depends on Rails core accepting this feature once someone provides a PR. I think you can still request features here, but someone will have to do the work of creating a patch as well. :thinking:

I understood. Thanks for your reply.

Same issue here, I was expecting this issue was solved by ActiveStorage rather than the deprecated PaperClip.

If we have many models with attaching objects, Rails Core expects to have all that images in the same folder?

@laranicolas That is my understanding too, ActiveStorage is not meant to provide file organization within the storage service so everything lives at the root of the bucket (in the case of S3).

  • we may lose connections between database tables to cloud filesystems. somebody can lose some data in active_storage_blobs without purging s3 objects, or somebody can drop tables when the outgoing network is not working by mistake. In that case, we can not make clean the buckets’ object. I know that AWS S3 don’t care how many orphaned files in that bucket, but those cases are not so cool.
  • sometimes, we should share buckets with other system(not ruby on rails). In that case, buckets will be messed with active_storage rooted objects(if other system uses prefixes/folders)

In my case, I made a buckets only for active_storages, but not everyone can make buckets like me.

I thinks it will be nice if we can use prefixes(optionally) on active_storages

@kikikiblue I don’t want to force you into anything but sometimes the best way to contribute to open-source is to “scratch your own itch” as they say.

If you’re interested in working on a patch for this please let me know! I am happy to help anyone on their way to contributing to Rails! :tada:

1 Like