Rails Rails 4.1.2 Eager Load Doesn't Kill N+1 Query

jun1st · 2014年09月20日 · 2186 次阅读

原帖地址: http://stackoverflow.com/questions/25938111/rails-4-1-2-eager-load-doesnt-kill-n1-query#comment40607271_25938111

Rails: 4.1.2, Ruby: 2.1.1

I have an Article class, which is commentable and has_many :comments

module Commentable
   extend ActiveSupport::Concern

   included do
     has_many :comments, :as => :commentable
   end
 end

 class Comment < ActiveRecord::Base

   belongs_to :commentable, :polymorphic => true, :touch => true
   belongs_to :user

   #other codes....
 end

when display the article, I'd like to load all releated information

@item = Article.includes(:tags, {:comments => :user}).where(id: params[:id]).first

and it does preload tags and comments object.

but when I render comments, the comments object and user objects get loaded again.

= render @item.comments

from log:

![development log][1]

[1]: http://i.stack.imgur.com/LH98X.png

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号