はじめに
Active Storageを用いて画像を呼び出す際にN+1問題に直面したんで、解決方法を書き残しておきます。Active Storageの概要はこちらからどうぞ。
with_attached_attachment_nameを使う
Active Storageにはwith_attached_attachment_nameというスコープが存在します。
with_attached_attachment_name内ではincludes("#{name}_attachment": :blob)のように関連付けられたblobをincludeしています。以下のbookを例に見ていきましょう。
class Book < ApplicationRecord
has_one_attached :book_image
end
has_one_attachedメソッドを使ってActive Storageにbook_imageをアタッチメントしました。これによりwith_attached_book_imageというスコープが作成されます。このwith_attached_book_imageをallの代わりに使用します。
allでBookを全件取得した場合
class BooksController < ApplicationController
def index
@books = Book.all
end
with_attached_book_imageでBookを全件取得した場合
class BooksController < ApplicationController
def index
@books = Book.with_attached_book_image
end
まとめ
Active Storageを扱う際は関連付けられたblobをincludeするwith_attached_attachment_nameを使いましょう。

