2014-09-19 5 views
2

레일을 죽여하지 않습니다 : 4.1.2, 루비 2.1.1레일 4.1.2 열망로드는 N + 1 개 쿼리

을 내가 commentablehas_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 

Article 클래스를 가지고 기사를 표시 할 때 모든 관련 정보를로드하고 싶습니다.

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

이며 태그 및 주석 객체를 미리로드합니다.

그러나 comments을 렌더링하면 설명 개체와 사용자 개체가 다시로드됩니다. 로그에서

= render @item.comments 

:

development log

+0

레일 버전을 지정하십시오. –

+0

@ Зелёный updated, thanks – fengd

+0

덕분에, 나는 같은 문제 (열망하는 다형성 레코드로드). +1 –

답변

0

마지막으로, 나는 Railspolymorphic 연결을 포함 할 수 없습니다 것을 알아 냈다. preload을 호출해도 가능한 한 적은 SQL 문으로 관련 모델이로드되지만 관련 모델에 대한 쿼리는 허용되지 않습니다.