简体   繁体   中英

N+1 queries in ModelSerializer

Comment.includes(:replies).without_replies

This way I get all the comments. I write API. And I use ActiveModelSerializer`

Comment have a relationship with the user. belongs_to :user

class CommentSerializer < ActiveModel::Serializer
  has_many :replies, class_name: 'Comment'

  attributes :id,
             :user_image_url

  def user_image_url
    object.user.image_url
  end
end

I need to get a picture of the user who left a comment. Method user_image_url . It's all good. But the bullet displays a message.

GET /api/v1/comments?page=1&per_page=20
USE eager loading detected
  Comment => [:user]
  Add to your finder: :includes => [:user]
Call stack
  /app/serializers/comment_serializer.rb:27:in `user_image_url'

I did this. Comment.includes(:replies, :user).without_replies But nothing, why?

您是否尝试过Comment.includes(:replies, :user).without_replies吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM