Rails 5.2 added a built-in solution for handling file uploads using Active Storage.
Let’s say, we have an Candidate
model that has two attachments resume
and
cover_letter
.
Before Rails 6.1, only one cloud storage service (Amazon S3, Google cloud storage, Microsoft Azure storage) could be used for overall application.
But, there could be cases where our application might require to upload images to different storage services.
In Rails 6.1
For the above example, let’s say we want to upload resume
to Amazon S3
and cover_letter
to Google cloud
.
We need to pass service
parameter to has_one_attached
method as shown
below:
In the above example, resume
of the candidate is stored to amazon
and cover_letter
to google
.
storage.yml
can be configured as below for multiple storage services:
NOTE:
-
If the
service
is not specified the defaultservice
set inconfig/environments
is used for storage. -
If specified service is not properly configured,
has_one_attached
would throw an error. -
If our project is already using Active Storage and when we upgrade to Rails 6.1, we should run
rake app:update
to make sureservice_name
column is added to the internalActiveStorageBlob
model.