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".
Do you know if it's possible (or advisable) to use ActiveStorage with Rails 5.1.6? In the Rails guides it states:
> After upgrading your application to Rails 5.2
http://guides.rubyonrails.org/active_storage_overview.html#setup
So I wonder whether it would be ill advised to add ActiveStorage as a gem to 5.1.6.
I haven't tried adding it to an "older" version of rails. It does seem to be fairly decoupled from Rails as its own gem.
Though, one weird thing that they do which is a little strange that could prevent this from easily working is in the ActiveStorage gemspec.
```ruby
s.add_dependency "actionpack", version
s.add_dependency "activerecord", version
```
It has this reference to version which is in the main branch's `RAILS_VERSION` file. If you reference a `5.1.6` tag, the ActiveStorage gem wouldn't exist. You may need to clone the repo and manually adjust it. This would inherently add a level of technical debt as you would have to migrate any bugfixes/security patches/enhancements to your branch.
Yikes, it sounds like it's not the best idea for a simple plug and play solution with 5.1.6 then.
Thanks for taking the time to reply and thanks for the great video.
Thanks a lot for this video. Always great.
I'm fighting to get drag'n drop working, did you tried as well ?
http://edgeguides.rubyonrails.org/active_storage_overview.html#integrating-with-libraries-or-frameworks
Hi Dave,
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.
In this same example with a user and a image, we can get access to the image afterwards.
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:index
banned: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).
Hi Dave,
Many thanks! yes, I have adjusted both solutions for my application and both worked as I needed.
Kind regards!
Very cool!