【问题标题】:Active model Serializers not eager loading relationships活动模型序列化器不急于加载关系
【发布时间】:2017-12-04 21:26:17
【问题描述】:

我正在使用 Bullet gem 来查看我的应用程序中有 n+1 个查询的位置。它告诉我在调用我的序列化程序时渴望加载我的关联taggings。我的代码如下所示:

render json: @products, each_serializer: ::V1::ProductSerializer, includes: [:taggings], links: links, status: :ok

但是在我添加之后,我仍然收到来自 Bullet gem 的相同警告。看起来像这样:

GET /api/v1/product_feed?state=CA&page=1
USE eager loading detected
  Product => [:taggings]
  Add to your finder: :includes => [:taggings]
Call stack
  /home/jay/current_projects/api/app/controllers/api/v1/products_controller.rb:111:in `product_feed'

有谁知道为什么标签表没有被急切加载。

【问题讨论】:

    标签: ruby-on-rails ruby eager-loading active-model-serializers


    【解决方案1】:

    您需要首先在查询中包含标记,然后序列化程序将能够读取加载的记录,而不是单独逐条询问标记关联记录

    @products = Product.includes(:taggings)
    render json: @products, each_serializer: ::V1::ProductSerializer, includes: [:taggings], links: links, status: :ok
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-21
      • 2019-09-27
      • 1970-01-01
      • 2018-09-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多