[RUBY] [Active Storage] Validation settings when uploading files


Since Active Storage does not have default validation, it records the time when you set the validation by yourself as a memorandum.


・ Ruby '2.5.7' ・ Rails '5.2.3'


1. Preparation

Execute the following command to install Active Storage.

$ rails active_storage:install

A migration file is created that creates the following two tables. ・ Active_storage_attachments ・ Active_storage_blobs

Rails db: migrate to create the table.

$ rails db:migrate

2. Association to model

We will associate it with the model. Here, it is assumed that multiple profile images are set in the User model.


class User < ApplicationRecord
  has_many_attached :avatars

: avatars is the name of the file, and you can specify whatever you like, such as : photos, : images, etc. according to the purpose of the file. There is no need to provide columns for images in the User model.

3. Validation settings

We will set the validation for the User model. Here, we will make the following three settings.

・ Avatar_type Specify the file format that can be uploaded.

・ Avatar_size Specify the size (capacity) of one file that can be uploaded.

・ Avatar_length Specify the number of files that can be uploaded.


class User < ApplicationRecord
  validate :avatar_type, :avatar_size, :avatar_length


  def avatar_type
    avatars.each do |avatar|
      if !avatar.blob.content_type.in?(%('image/jpeg image/png'))
        errors.add(:avatars, 'Please upload in jpeg or png format')

  def avatar_size
    avatars.each do |avatar|
      if avatar.blob.byte_size > 5.megabytes
        errors.add(:avatars, "Must be within 5MB per file")

  def avatar_length
    if avatars.length > 4
      errors.add(:avatars, "Please keep within 4 sheets")

By the way, I am deleting temporary data with `ʻavatars.purge``. If a validation error occurs in other input items, the uploaded file will be saved in the storage when it is assigned to the attribute of the model instance in Rails 5.2 to 6.0 series or earlier, and as a result, it will be in the blob. This is because temporary data for errors may accumulate. (However, there was a change in the function of Active Storage in the update to Rails 6.0, and after the save method was executed, it was changed to the specification to be saved in the storage, so it is unnecessary if it is 6.0 or later)


Now you have the following validation settings! 200919_バリデーションエラー.png


Consider whether Active Storage can be used as a substitute for Carrier Wave Implement image upload function using Rails5 Active Storage

Recommended Posts

[Active Storage] Validation settings when uploading files
Rails Active Storage shrinks images before uploading
Introduce Active Storage
About Active Storage
[Rails 6] Add images to seed files (using Active Storage)
About DelegationError (Active Storage)
Active Storage Initialization Manual
[Beginner] About Active Storage
A mistake made when displaying an image using Active Storage
[Rails] Create API to download files with Active Storage [S3]