Curated list of episodes, bundled to help you on your learning path!
Videos of tech, infrastructure, hardware and software which non-instructional by nature.
Articles around software and tutorials.
Show your love and support while looking fancy.
Got a question or want to chat about a topic? Let's talk!
Excellent post. I just have two questions regarding ActiveStorage:
- How can we manipulate (add prefixes, for example) the filename that has to be uploaded?
- How would you approach an scenario where you need to mark the attachments? I amb thinking, for example, in the case where you want to set some images as banned. As we cannot manipulate ActiveStorage tables, it looks very tricky to imagine a solution. maybe a proxy table? with your example, it would be something like "users" -> "files" -> "attachments". So you can always query for the "files" filtered and get the proper "attachments".
Thanks for the tutorial!
For the filename, it can be handled a few different ways. Let's say you have a user model and the user model has_one_attached :image
If you're manually uploading an image then you can override the filename at the time of attaching it.
uri = URI.parse(URL_OF_IMAGE);filename = File.basename(uri.path);io = open(uri);content_type = io.content_type;user.image.attach(io: io, filename: filename, content_type: content_type);user.save;
In this same example with a user and a image, we can get access to the image afterwards.
user.image.blob.filename=> "whatever.png"user.image.blob.filename = "newname.png"user.image.blob.save=> "newname.png"
For banning a file, I think you're heading in the right direction.
I would have a separate model for files that are uploaded for a user. Something like a User has_many Datei and Datei belongs to User.
The Datei model (german for file because I wouldn't want a model called File) would have a few attributes plus whatever else needed
user_id:integer:indexbanned:boolean, default: false
The model would have something like has_one_attached :document.
So now a user can have many files (Datei) and each file has an attached document. You can flag the file as banned (or whatever else params).
Many thanks! yes, I have adjusted both solutions for my application and both worked as I needed.