activerecordruby-on-rails-6has-and-belongs-to-manyrails-activestorage

How to eager_load attachments: 1+N problem not working with ActiveStorage attachments


I have a product list which gets serialized for an JSON API response

For that I need also imageUrl which come from ActiveStorage attachments. I stumbled over the 1+N problem.

As you can see, I already tried to include any kind of 'includes' And I already added the 'with_attached_photos'

How can I fix this 1+N problem?

model:

class Product
  ActiveStorage::Current.host = ENV['APPLICATION_URL']
  include Rails.application.routes.url_helpers

  has_many_attached :photos
  has_and_belongs_to :categories

  def photo_thumbnail_urls
    photos.map do |photo|
      # this generates urls for the API
      rails_representation_url(photo.variant(thumb_options), host: ActiveStorage::Current.host)
    end
  end
end

And this is my controller:

class ProductsController < ApplicationController

  # as you can see, I already tried to include any kind of 'includes' 
  # and I already added the 'with_attached_photos' 

  top_rated_products = Product.includes(:categories,
                                        :active_storage_blobs,
                                        :categories_products,
                                        :active_storage_attachments,
                                        photos_attachments: :blob,
    where("lower(name) LIKE lower(?)", "%" + query_param + "%").
    where("categories_products.category_id": [1,2,3,4]).
    with_attached_photos

  render json: top_rated_products, each_serializer: TopProductSerializer

end

and this is my Serializer

class TopProductSerializer < ActiveModel::Serializer
  attribute :id
  attribute :name
  attribute :photo_thumbnail_urls, key: :images
end

But my console is full of this queries:

 Product Load (4.2ms)  SELECT "products".* FROM "products" WHERE (lower(name) LIKE lower('%ha%')) ORDER BY created_at asc LIMIT $1  [["LIMIT", 5]]
  ↳ app/controllers/products_controller.rb:116:in `top_rated'
  HABTM_Categories Load (0.3ms)  SELECT "categories_products".* FROM "categories_products" WHERE "categories_products"."product_id" IN ($1, $2, $3, $4, $5)  [[nil, 740], [nil, 741], [nil, 742], [nil, 744], [nil, 746]]
  ↳ app/controllers/products_controller.rb:116:in `top_rated'
  Category Load (0.2ms)  SELECT "categories".* FROM "categories" WHERE "categories"."id" IN ($1, $2)  [[nil, 100], [nil, 5107]]
  ↳ app/controllers/products_controller.rb:116:in `top_rated'
[active_model_serializers]   ActiveStorage::Attachment Load (0.2ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3  [["record_id", 740], ["record_type", "Product"], ["name", "photos"]]
[active_model_serializers]   ↳ app/models/product.rb:53:in `photo_thumbnail_urls'
[active_model_serializers]   ActiveStorage::Blob Load (0.1ms)  SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 3840], ["LIMIT", 1]]
[active_model_serializers]   ↳ app/models/product.rb:55:in `block in photo_thumbnail_urls'
[active_model_serializers]   ActiveStorage::Blob Load (0.2ms)  SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 3841], ["LIMIT", 1]]
[active_model_serializers]   ↳ app/models/product.rb:55:in `block in photo_thumbnail_urls'
[active_model_serializers]   ActiveStorage::Blob Load (0.1ms)  SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 3842], ["LIMIT", 1]]
[active_model_serializers]   ↳ app/models/product.rb:55:in `block in photo_thumbnail_urls'
[active_model_serializers]   ActiveStorage::Blob Load (0.1ms)  SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 3843], ["LIMIT", 1]]
[active_model_serializers]   ↳ app/models/product.rb:55:in `block in photo_thumbnail_urls'
[active_model_serializers]   ActiveStorage::Blob Load (0.2ms)  SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 3844], ["LIMIT", 1]]
[active_model_serializers]   ↳ app/models/product.rb:55:in `block in photo_thumbnail_urls'
# and so on and so on... 
#......
#....
#...
#..
#.

What else did I miss?


Solution

  • I got it working with:

    class Product
      scope :eager_load_photos, -> { includes photos_attachments: :blob }
    end
    
    Product.eager_load_photos.where(......).limit(n) # and so on
    

    The 1+N problem is now completly gone