Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
40 lines (31 sloc) 1.22 KB
layout title categories published meta
Storing a String in ActiveStorage
– blog
description index
Rails ActiveStorage is a great way of saving files, but you can also store blobs of text.

Recently, I came across a useful way to use ActiveStorage. I had a text field on my model, that stored about 200 kB of data that was only being read a handful of times. A quick estimate suggested this single column contained about 90% of the databases content.

The solution was to offload this columns data to S3 via Rails ActiveStorage, and only load it into memory as we required it.

The Code

I used the StringIO class to convert my string to a blob, then just attached that to my model. When I wanted to read it, I just downloaded the file from S3 and assigned the contents to an instance variable.

class Report < ApplicationRecord
  has_one_attached :raw_data

  validates :raw_data, presence: true

  def data=(value)
    @data = value
      filename: 'raw_data.txt',
      content_type: 'text/plain'

  def data
    @data ||=
You can’t perform that action at this time.