Tutorial on how to use Active Storage on Rails 6.2
Tags: ruby, rails, webdev, tutorial
Published: 2021-09-13 07:13:34

---
title: Tutorial on how to use Active Storage on Rails 6.2
published: true
description: A new rails tutorial - How to use active storage in Rails 6.2. Covered everything about Active Storage - from definition to installation to functionalities to major updates in recent releases. Chech it out
tags: Ruby, Rails, webdev, tutorial
canonical_url: https://blog.engineyard.com/tutorial-active-storage-on-rails-6.2?utm_source=dev.to&utm_medium=Devgrap-page&utm_id=QiWorks
//cover_image: https://blog.engineyard.com/hubfs/Blog-3-test-2.jpg
---
By [Ritu Chaturvedi](https://blog.engineyard.com/author/ritu-chaturvedi)

# Understanding Active Storage in Rails 6.2
Active storage is an inbuilt gem in Rails that developers widely use to handle file uploads. Combined with the encrypted credentials feature in the latest releases of Rails, active storage is a safe and easy method to upload, serve, and analyze files onto cloud-based storage services as well as local storage.

To start with, we need to install the [active storage gem](https://guides.rubyonrails.org/active_storage_overview.html). This step is followed by declaring attachment associations, uploading attachments, processing attachments, and adding validations.

## Active storage gem Installation

With any new application, the first step to enable active storage is to [install the gem](https://blog.engineyard.com/how-to-build-your-own-gem-in-ruby). Run the below to install this migration to create the three basic tables automatically:

bin/rails active\_storage:install

 

Run the migration by below:

bin/rails db:migrate.&nbsp;

 

This creates the three tables for your application as active\_storage\_blobs, active\_storage\_variant\_records, and active\_storage\_attachments. Out of these three, the active\_storage\_attachments is a polymorphic join table.&nbsp;

 
- The blobs table holds some straightforward details about the uploaded file like filename and content type. It also stores the encoded key that points towards the uploaded file in the active storage service.
- The _active\_storage\_attachments_ table is a polymorphic join table that holds references to a _blob_ and a _record_. The polymorphic option is set to true in t.references:record, &nbsp; null: false, polymorphic: true, index: false line when this table was created. This is set this way because the _record\_id_ column has details on what type of data it is holding. The identification is made clear with the help of a foreign key and a class name defined in the table.
- Usually, active storage stores original copies, but it also lets the user accommodate modifications like resizing file size. The _active\_storage\_variant\_records_ table holds details about all these modified files.

## Functionalities of Active Storage

Active storage gem is used to attach, remove, serve, and analyze files.&nbsp;

 

__Attaching files:__ Files can be attached as a single file or multiple files. Use macros like ‘_has\_one\_attached_’ and ‘_has\_many\_attached_’ accordingly.&nbsp;

 

Below are the sample codes to add attachments.

 

One attachment:

_class_ **_User_**  **_\<_** _ApplicationRecord_

_&nbsp;&nbsp;has\_one\_attached_ _:avatar_

_End_

Many attachments:

_class_ **_Message_**  **_\<_** _ApplicationRecord_

_&nbsp;&nbsp;has\_many\_attached_ _:images_

_End_

Active storage enables attaching files and data to record on storage services. If we expand the ‘_has\_one\_attached_’ declaration, we can see that there is an _avatar\_attachment_ and an _avatar\_blob_ to get through the avatar\_attachment association.&nbsp;

 

[Third-party services](https://edgeguides.rubyonrails.org/active_storage_overview.html) are used to open, view, and operate the attached files from storage services. The content type of attachment decides the kind of service to be used.&nbsp;

 

If the _avatar\_attachment_ is an image file attachment, here’s how you can upload an image to this model.&nbsp;

 

_\<%=_ f.label :avatar _%\>_

_\<%=_ f.file\_field :avatar _%\>_

 

In order to display the uploaded image, run:

_\<%=_ image\_tag event.avatar _%\>_

 

It is always advisable to add [custom validations](https://guides.rubyonrails.org/active_record_validations.html#custom-validators) to the files uploaded since the Active storage feature does not include in-built validations. File type and the file size must be validated before the upload, to avoid errors and complications.

 

As discussed above, Rails allows modification of the uploaded files and stores the data in the _variants_ table. For example, to process image files, the **_image\_processing_** gem of Rails can be used.

 

Remember to encrypt your storage service credentials before uploading to the cloud. Encrypted credentials are a safe way to handle cloud-based storage services like Amazon S3.&nbsp;

 

**Removing files:** The attached files can also be removed from the records by using the purge command.

User. **avatar**. **purge**

**Serving files:** The uploaded files can be served by active storage. Two methods are used for this - the redirecting method and the proxying method. The redirect method uses the file’s blob URL to serve the file, and the proxying method will download data in files from the storage service.

 

As mentioned, active storage uses third-party software to enable file processing. You can download and install [_libvips_](https://github.com/libvips/libvips)or [_ImageMagick_ v8.6+](https://imagemagick.org/index.php) for image analysis and transformations, [_ffmpeg_ v3.4+](http://ffmpeg.org/) for video/audio analysis and video previews, and [_poppler_](https://poppler.freedesktop.org/)or [_muPDF_](https://mupdf.com/)for PDF previews separately, as Rails will not install this software.

## **Major active storage updates in recent releases**

In the recent [releases of Rails](https://weblog.rubyonrails.org/releases/), the active storage gem has seen notable updates. The major ones are as follows:

- In _ffmpeg_, _the_ user can configure all those parameters used for generating a video preview image under _config.active\_storage.video\_preview\_arguments._
- No error is raised when the _mime_ type is unrecognizable.
- When an image previewer cannot generate a preview image, _ActiveStorage::PreviewError_ is raised.
- Even with no service selected, Blob creation will not crash.
- Like MuPD previewer, Poppler PDF previewer also uses the original document's crop box to display a preview image.&nbsp;

- Configure attachments for the service you want to store them in.
- Use any URL, private or public, for blobs. The latest Rails update on active storage extends support for this feature and ensures the public URLs are permanent.&nbsp;&nbsp;
 

Now that you have seen how to install the active storage gem, explore its functionalities, releases, updates, and uploading files, we hope that this blog helped you gain a better understanding of active storage in Rail 6.2.