Beginner Guide to Active Storage

Jakob Persons
4 min readMay 5, 2021

Over the pasts few years, Active Storage has taken the place of Paperclip within Ruby on Rails. Paperclip is no longer actively supported, where as Active Storage is. Not to mention that they offer similar solutions, so the transitions makes sense. Active Storage is now the preferred way for developers to use cloud based storage. It also allows for local disk storage for development and testing environments.

Why use Active Storage?

Developers find it easier and more efficient to have the ability to attach files to Active Record objects as they desire, rather than allocating rows for numerous tables in their database. Active Storage also allows for transformation of images using ImageMagick (or the RMagick[github] gem library), and conversion to image-like files from which metadata can be retrieved. For example, one might use the RTesseract[github] gem in order to get OCR data from an accounts payable worksheet.

Install and Setup

The first step is to install active storage within your Ruby application (assuming you are using v5.2 or later). From the root directory of your project, you can install Active Storage with the following terminal command:

rails active_storage:install

This is going to result in the creation of a migration, in which Active Record will create two tables to be included in your database. active_storage_blobs and active_storage_attachments — read more about Active Storage Blobs and Active Storage Attachments here: [ActiveStorageBlobs] [ActiveStorageAttachments]

Understanding what these two tables represent and how they are connected with each other and Active Record objects is the meat of Active Storage.

Next, you’ll need to configure your Active Storage setup.

In your config/storage.yml you’ll need to declare storage services for specific environments. Your config/storage.yml file should look like this:

config/storage.yml

Above, we are telling Active Storage that both the local and test environments will use the machine’s desk for storing data. The third section is declaring Amazon’s S3 cloud service as the storage location for the production environment.

After declaring storage for your separate environments, you’ll need to tell Active Storage which service to use in the respective environment files.

Sets Active Storage service to local from `config.environments.development.rb`
Sets Active Storage to amazon from ‘config.environments.production.rb`

Ruby documentation states best practice as setting services on per environment basis.

Attaching files to Active Record objects

Within one or more of your model classes, you can use the following macro’s for attachments:

has_one_attached -> creates a one to one relationship between the Active Record object and an attachment

a User object can have one attachment called ‘avatar’

Within your controller:

Note: you must add the attachment to params!

has_many_attached -> creates one to many relationship between the Active Record object and attachments

a User can have many attachments called ‘images’

Within your controller:

Note: within params, images is set as an empty array that will contain all image attachments

There are a couple built in methods to help out with Active Record attachments. First .attach — is used to set an attachment to an Active Record object. Make sure you give an argument of (params[:attachment_name]. Second, .attached? — will return boolean depending on whether and Active Record object has an attachment or not. Finally, .purge — will delete or remove attachment from given Active Record object.

You can do many other things with Active Record attachments as well. All of which are well documented in the Ruby Guides Active Storage documentation. Examples include, linking to files, transforming images and previewing files. Direct uploads are also covered.

Cloud service options for Active Record

You can find configuration setup for Active Record at the Ruby Guides documentation here.

Amazon S3

Microsoft Azure

Google GCS

Remember that Active Storage is now the go to for cloud storage within Ruby on Rails applications. With setups and configuration for development, test and production environments — Active Storage is able to provide all your cloud storage services.

--

--

Jakob Persons

Software Engineer | Full Stack Developer | Soccer Fanatic