Active Storage ActiveStorage::Attached

site.rb

class Site < ApplicationRecord
  has_one_attached :og_image
  has_one_attached :favicon
  has_many_attached :main_images
end
  • favicon:アイコン画

  • og_image:画像を1枚添付できる

  • main_images:画像を複数枚添付できる

これらをrails consoleで見てみましょう

[25] pry(main)> site.favicon
=> #<ActiveStorage::Attached::One:0x00007fd76d0579a8
 @dependent=:purge_later,
 @name="favicon",
 @record=
  #<Site:0x00007fd76aeef9a0
   id: 1,
   name: "Blog",
   subtitle: "Very awesome!",
   description: "",
   created_at: Fri, 02 Jul 2021 07:40:50 JST +09:00,
   updated_at: Fri, 02 Jul 2021 08:44:57 JST +09:00>>
[27] pry(main)> site.og_image
=> #<ActiveStorage::Attached::One:0x00007fd76c532938
 @dependent=:purge_later,
 @name="og_image",
 @record=
  #<Site:0x00007fd76aeef9a0
   id: 1,
   name: "Blog",
   subtitle: "Very awesome!",
   description: "",
   created_at: Fri, 02 Jul 2021 07:40:50 JST +09:00,
   updated_at: Fri, 02 Jul 2021 08:44:57 JST +09:00>>
[28] pry(main)> site.main_images
=> #<ActiveStorage::Attached::Many:0x00007fd764515480
 @dependent=:purge_later,
 @name="main_images",
 @record=
  #<Site:0x00007fd76aeef9a0
   id: 1,
   name: "Blog",
   subtitle: "Very awesome!",
   description: "",
   created_at: Fri, 02 Jul 2021 07:40:50 JST +09:00,
   updated_at: Fri, 02 Jul 2021 08:44:57 JST +09:00>>

ここで気になったのがActiveStorage::Attached::OneとActiveStorage::Attached::Manyさらに、@dependent、@name、@recordです。



ActiveStorage::Attached::OneとActiveStorage::Attached::Many

site.rbでhas_one_attachedで定義された添付ファイルはActiveStorage::Attached::One

ActiveStorage::Attached::One

has_many_attached で定義された添付ファイルはActiveStorage::Attached::Manyです。

ActiveStorage::Attached::Many

ActiveStorage::Attachedについては、以下の記事が参考になります。ざっくり説明すると、ActiveStorage::Attached::OneおよびActiveStorage::Attached::Manyクラスの抽象ベースクラスで、レコードのblob関連へのプロキシアクセスを提供します。

api.rubyonrails.org



@dependent、@name、@record

そもそもこの3つはどこで定義されているの?

探してみるとありました!

$ rails active_storage:install

↑を実行したときに、このファイルが作られるみたいです。ちなみにprivate以下は、コードを書き換えています。 attached.rb

# frozen_string_literal: true

require "action_dispatch"
require "action_dispatch/http/upload"
require "active_support/core_ext/module/delegation"

module ActiveStorage
  # Abstract base class for the concrete ActiveStorage::Attached::One and ActiveStorage::Attached::Many
  # classes that both provide proxy access to the blob association for a record.
  class Attached
    attr_reader :name, :record, :dependent

    def initialize(name, record, dependent:)
      @name, @record, @dependent = name, record, dependent
    end

    private
      def create_blob_from(attachable)
        case attachable
        when ActiveStorage::Blob
          attachable
        when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile
          ActiveStorage::Blob.create_after_upload! \
            io: attachable.open,
            filename: attachable.original_filename,
            content_type: attachable.content_type
        when Hash
          ActiveStorage::Blob.create_after_upload!(attachable)
        when String
          ActiveStorage::Blob.find_signed(attachable)
        else
          nil
        end
      end
  end
end

require "active_storage/attached/one"
require "active_storage/attached/many"
require "active_storage/attached/macros"