ActiveStorage: How to add a new preprocessed named version

Posted . Visible to the public.

Given there is a user with an attachable avatar:

class User < ApplicationRecord
  has_one_attached :avatar
end

If you want to add a preprocessed version follow these steps:

  1. Add the named version and deploy
class User < ApplicationRecord
  has_one_attached :avatar do |attachable|
    attachable.variant :preview, resize_to_fit: [177, 177 * 9 / 16], preprocessed: true
  end
end
  1. Preprocess this version for all existing records bundle exec rails runner 'User.find_each { |user| user.avatar.variant(:preview).processed }'
  2. Use the new variant and deploy
Last edit
Arne Hartherz
Keywords
active, storage, reprocess
License
Source code in this card is licensed under the MIT License.
Posted by Emanuel to makandra dev (2025-02-24 15:41)