活动模型序列化器,而不是热切加载关系

时间:2020-12-17 20:13:25

I'm using the Bullet gem to see where there are n+1 queries in my application. It's telling me to eager load my association taggings when calling my serializer. My code looks something like this:

我正在使用Bullet gem查看应用程序中n+1查询的位置。它告诉我在调用我的序列化器时加载我的关联标记。我的代码是这样的:

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

But after I add that, i'm still getting the same warning from the Bullet gem. Which looks like this:

但在我加了这个之后,我仍然从子弹宝石中得到同样的警告。它看起来像这样:

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'

Does anyone have any idea why the taggings table is not being eager loaded.

有没有人知道为什么标签表没有加载。

1 个解决方案

#1


4  

You need to include taggings in your query first then the serializer will be able to read the loaded records instead of separately asking for the taggings association record by record

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

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

#1


4  

You need to include taggings in your query first then the serializer will be able to read the loaded records instead of separately asking for the taggings association record by record

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

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