简体   繁体   English

Ruby on Rails - 子弹宝石。 添加到您的查询中: .includes([:author])

[英]Ruby on Rails - Bullet Gem. Add to your query: .includes([:author])

I have an N+1 Query issue, but I dont understand how to fix the issue.我有一个 N+1 查询问题,但我不明白如何解决这个问题。

I have a User model, Post model and Comment Model.我有一个用户模型、帖子模型和评论模型。

User has many posts 
User has many comments 

Post belongs to User
Post has many Comments 

Comments belong to user 
Comment belongs to post

In my post controller, I have在我的后控制器中,我有

def index
    @posts = Post.includes(:author).where(author_id: params[:user_id])
end


def show
    @post = Post.includes(:author).find(params[:id])
end

When I load the Posts show view below...当我加载下面的帖子显示视图时...... 在此处输入图像描述

I get these two warnings from Bullet Gem我从 Bullet Gem 收到这两个警告

user: espada
GET /users/2/posts/6
USE eager loading detected
  Comment => [:author]
  Add to your query: .includes([:author])
Call stack

user: espada
GET /users/2/posts/6
USE eager loading detected
  Comment => [:post]
  Add to your query: .includes([:post])
Call stack

I have tried to add the two .includes in various actions, tried many things for the past 5 hours...nothing works.我尝试在各种操作中添加这两个 .include,在过去 5 小时内尝试了很多事情......没有任何效果。 At this point, I want to understand where do I add those two includes?此时,我想了解在哪里添加这两个包含?

And how does one interpret that message?以及如何解释这一信息?

Its showing an error on the comments model它在评论模型上显示错误

Try This尝试这个

def index
    @posts = Post.includes(:author, comments: %i[author post]).where(author_id: params[:user_id])
end


def show
    @post = Post.includes(:author, comments: %i[author post]).find_by_id(params[:id])
end

You're probably looking for a dependency on comments, not on posts.您可能正在寻找对评论的依赖,而不是对帖子的依赖。 So you have to include it like this.所以你必须像这样包含它。

Post.includes(:author, comments: [:author, :post]).find(params[:id])

Thanks to Larkin who mentioned that the problem could be in the view, I went and looked at my Post index and Post show view.感谢Larkin提到问题可能出在视图中,我去查看了我的 Post index 和 Post show view。

In my post show and index views, I was rendering a partial for the comments with在我的帖子显示和索引视图中,我渲染了部分评论

<%= render partial: "shared/post/comment_card", locals: { post: @post, comments: @post.comments} %>

In my comment card I had a delete button在我的评论卡中,我有一个删除按钮

<%= button_to "Delete Comment", user_post_comment_path(comment.author, comment.post, comment), 
      method: :delete, data: { confirm: "Are you sure?" } %>

That is where the warning was coming from when I get the post for each comment.当我收到每条评论的帖子时,这就是警告的来源。 I realized that since I had passed in the post via locals I could write my delete button as我意识到,由于我是通过本地人在帖子中传递的,所以我可以将删除按钮写为

<%= button_to "Delete Comment", user_post_comment_path(comment.author, post, comment), 
          method: :delete, data: { confirm: "Are you sure?" } %>

That fixed that error.这解决了这个错误。

However, I also had to change my show action because trying to get a post without comments was raising a warning, so I wrote it as然而,我也不得不改变我的表演动作,因为试图获得一个没有评论的帖子会引发警告,所以我把它写成

  def show
    @post = Post.includes(:author).find(params[:id])
    @post = Post.includes(:author, comments: [:author]).find(params[:id]) if @post.comments.any?
  end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails - “将&#39;gem sqlite3&#39;&#39; 添加到您的 Gemfile” - Ruby on Rails - "Add 'gem sqlite3'' to your Gemfile" 使用回形针gem的错误(before_avatar_post_process)。 我正在使用Rails 4.2和ruby 2 - Error(before_avatar_post_process) using paperclip gem. I am using rails 4.2 and ruby 2 Ruby on Rails Geocoder宝石。 列出附近搜索返回的令人困惑数据的结果 - Ruby on Rails Geocoder Gem. List results of nearby search returning confusing data Ruby on Rails,Devise gem。 密码为空时如何删除当前密码? - Ruby on Rails, Devise gem. How to remove current password when password is blank? Rails西装外套宝石。 该地图未显示 - Rails Blazer Gem. The map is not shown 邪恶宝石的轨道。 数据不会更新 - Rails with Wicked gem. Data will not update 是类似bullet的gem行为,可以自动检测查询性能并建议您在某些特定列上添加索引 - Is a gem acts like `bullet` which can automatically detect your query performance and suggest you to add an index on some specific columns 缺少Rails宝石。 请`gem install -v = 2.3.4 rails` - Missing the Rails gem. Please `gem install -v=2.3.4 rails` Ruby on Rails错误:数据库适配器指定为“ mysql2”,但未加载gem。 将`gem&#39;mysql2&#39;`添加到您的Gemfile - Ruby on Rails error: Specified 'mysql2' for database adapter, but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile 带有Adhoq宝石的Rails 5。 无法解决捆绑程序依赖性 - Rails 5 with Adhoq gem. Unable to resolve bundler dependencies
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM