DEV Community

Cover image for The Ultimate Guide to Active Storage in Rails
Nemwel Boniface
Nemwel Boniface

Posted on • Updated on

The Ultimate Guide to Active Storage in Rails

What is Active storage?

When developing an application, you must have faced a situation where you want to include a file upload functionality eg allow your users to upload their profile pictures when they are signing up. Implementing this feature on your own proves to be a cumbersome process and this is where using a mature framework like Ruby on Rails comes to pay dividends.

Active Storage is an inbuilt library in Ruby on Rails that provides an easy way to upload files and associate them with models in a Ruby on Rails application.

It offers an easy way to integrate file uploads from your local machine and store them in both the local disk storage or in external cloud storage service providers such as Cloudinary and simplifies the retrieval and processing of the same files that were uploaded.

Part B of the series is available and can be found here.

Before we continue

Before we go deep learning about active storage

While most of us have been rushing to meet our specific needs (Making the file upload functionality work), most of us don't know what constitutes Active storage.

If you're looking to take your Ruby on Rails skills to the next level and learn about working with files, then you've come to the right place. I plan to make this multiple series a one-stop guide for understanding what Active Storage is, its ins and outs, how to setup Active storage in your application covering both file uploads and retrieval to showcase the retrieved file to your screen, and in the last series, I shall walk you through using Active Storage to upload your images to Cloudinary.

This is the first part of the series and in this version of the series, I aim to give an in-depth overview of Active Storage. This article you are reading now covers the theory behind Active Storage and we shall work on the technical setup in our application in part B of the article series.

Active Storage 101

Active Storage makes use of three tables in your application's database namely: active_storage_blobs, active_storage_attachments, and active_storage_variant_records, which are generated when running the command rails active_storage:install to create a migration that will generate the above-mentioned three tables. We shall discuss the three tables in depth below.

1. active_storage_blobs

The active_storage_blobs table migration generated when running rails active_storage:install

This table is used to store metadata for the uploaded files where each row represents a record/ file that has been uploaded to your Rails application. The blobs table contains the following columns:

a. id - This is a unique identifier for the blob (file)
b. Key - This is a unique key that is used to retrieve the blob from storage
c. filename - This column contains the original file names of the uploaded files
d. Content_type - This column contains the MIME type of the uploaded file
e. Byte_size - This column contains the size of the uploaded file in bytes
f. Checksum - This column contains a SHA256 hash of the uploaded file. The SHA256 hash is used in Active Storage to ensure the integrity of the uploaded files

2. active_storage_variant_records

The active_storage_variant_records table migration generated when running rails active_storage:install

This storage is used by Active storage to store the metadata for variant files. In simple terms, the variant files are different versions of a file that has been uploaded and processed by Active Storage to give them a different size, format, or other properties.

When you use Active Storage to process an uploaded file such as resizing images, a new variant file is created and stored in the configured storage service eg local disk storage or an external cloud service such as Cloudinary.

Thus the active_storage_variant_records is used to store the metadata about these variant files including their "variation-digest", which is a unique identifier for the specific processing that was applied to that specific file. This table contains the following columns:

a. id - A unique identifier for the variant record
b. variation-digest - the unique identifier for the processing that was applied to that file
c. blob_id - This is the ID of the original file/ blob that a variant was created from
d. created_at - This is a timestamp for when a variant was first created
e. updated_at - This is a timestamp for when a variant was last updated

Overall, this table allows you to create and store variant files that have been processed from the original uploaded files and make the process of retrieval of the files efficient.

3. active_storage_attachments

The active_storage_attachments table migration generated when running rails active_storage:install

This is the table in active_storage that is used for associating uploaded files with records in our application's database.

When you make a file upload to active_storage, you can attach it to a record in your database using Rails associations has_one_attached (here, you mean that a profile_picture belongs to only one User), or has_many_attached (here you mean a User object can have many photos associated to them).

This table is used to efficiently retrieve the uploaded files associated with a particular record in our database. In case you call for eg user.profile_picture, Active_storage will lookup for a corresponding record in "active_storage_attachments" table, retrieve it and display it to you.

Metadata. What do I mean by Metadata?

Metadata refers to information that describes other data. Confusing huh? Stay with me here. In our conext of Active-Storage, metadata refers to the information that is stored about an uploaded file or a variant file such as the filename, the content_type, byte_size and the checksum etc.

Let's say I have a digital photo of myself saved in my local disk as "Nemwelheadshot.png". Metadata for my picture could include data such as: filename, date taken, camera model, aperture, shutter speed, ISO speed, etc.

All the information provided above provides additional context and detail about the photo and they help to organize and simplify the retrieval process of a file.

To sum it all up:

I have mentioned so much above and I would like to finalize this with a personalized summary of what each table's function is:

  1. The active_storage_blobs - this is the main engine of active storage as it stores all the core information about an uploaded file that is necessary for Active Storage to function properly.

  2. The active_storage_variant_records - this is the optimizer of the uploaded files by creating multiple versions of an uploaded file to eg optimize it to take up less space and make the process of file retrieval; faster and efficient.

  3. The active_storage_attachments - this is the middle man of active storage who links a specific uploaded file to a specific object in my database.

I hope that my short article has given you insights on what Active Storage is, what it helps you achieve, why you should use Active Storage in your Rails projects, and most importantly what constitutes Active Storage.

This one marks the end of the first series of The Ultimate Guide to Active Storage in Rails. Part B where I will walk you through the technical set up of Active storage locally in your application and later show you how to link it with Cloudinary to upload your images to an external cloud storage service provider will be coming soon. Be on the lookout for it!

That is it for today's class. I hope this was useful information for you. See you in my next article.

Top comments (5)

Collapse
 
nemwelboniface profile image
Nemwel Boniface

Hello @lojacho and @mykovasyl the second part of the series is ready. Here is the link. Thank you for your patience.

Collapse
 
lojacho profile image
Lojacho

Thanks for the tutorial, Where I can find the second part

Collapse
 
nemwelboniface profile image
Nemwel Boniface

Hello @lojacho l I am glad that you found my first part of the series insightful. I am working on the second part which will be released in the next week. I will tag you once published!

Collapse
 
mykovasyl profile image
Mykola Vasyl'yev

Great explanation! I've been struggling with uploading multiple files to an Assignment model so I REAAALLLYYYY need the next parts. Thank you!

Collapse
 
nemwelboniface profile image
Nemwel Boniface

Hello @mykovasyl I am glad that you found my first part of the series insightful. I am working on the second part which will be released in the next week. I will tag you once published!