1

与えられた

class User < ApplicationRecord
  has_one_attached :avatar

  def avatar_path
    Rails.application.routes.url_helpers.rails_blob_path avatar,
                                                         disposition: 'inline',
                                                         only_path: true
  end
end

class UsersController < ApplicationController
  def index
    @users = User.all
  end
end

各アバターを表示しようとするときにactive_storage_attachmentsN +1 クエリを回避するにはどうすればよいですか?active_storage_blobs

4

1 に答える 1

2

ActiveStorage の例に従って、 を使用できます#with_attached_A。ここAで、 は添付ファイルの名前です。

User.all.with_attached_avatar

同様に、たくさん付いている場合は、 を使用できます#with_attached_As

于 2018-06-21T14:01:58.673 に答える