What is Active Storage? and How to use Active Storage?

As per the Rails guide documentations:

Active Storage facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects. It comes with a local disk-based service for development and testing and supports mirroring files to subordinate services for backups and migrations.

So, Active Storage basically allow user to upload attachments (i.e. Images) to the remote storage clouds and and save object of the file into Active Record.

Earlier with Rails 3 and Rails 4, We had to upload the files using carrierwave gem. and With Rails 5.2  and after versions Rails has inbuilt feature for file uploading to cloud and save it’s object in Active Record using Active Storage module.

Which gem should be used for remote storage?

For Amazone web services (S3), you should use below gem:

gem "aws-sdk-s3", require: false

For Azure Cloud storage, you should use below gem:

gem "azure-storage", require: false

 For Google Cloud Storage, you should use below gem:

gem "google-cloud-storage", "~> 1.11", require: false


To Store files locally

config.active_storage.service = :local

To Store files locally

config.active_storage.service = :amazon

Use it as:

Upload file:

Using gems and configure that, You should use below lines to attache the avatar.


Delete file:


Using has_many_attached macros

The has_many_attached macro sets up a one-to-many relationship between records and files. Each record can have many files attached to it.
For example, suppose your application has a Product model. If you want each message to have many images, define the Product model like this:
class Product < ApplicationRecord
  has_many_attached :images
You can create a product with images:
class ProductsController < ApplicationController
  def create
    product = Product.create!(product_params)
    redirect_to product
    def product_params
      params.require(:product).permit(:title, :content, images: [])
Call images.attach to add new images to an existing message:
Call images.attached? to determine whether a particular message has any images:
Ref: Rails Guide

There are many other features included in it. Please go through the rails guide documentation.

Overall, It’s a great feature included in rails and was most awaited since Rails 3.

For more information, Please visit: https://edgeguides.rubyonrails.org/active_storage_overview.html  

About the author

Being the CEO and Founder of ClecoTech International, Mr. Ashish Prajapati is dedicated towards his aim of mentoring young startups into a full-fledged businesses. He is helping startups from America, Europe, India, and various other countries through proper guidance and the use of latest technologies to develop their innovation and ideas into definite realities.

Leave a Reply

Your email address will not be published. Required fields are marked *