I think maybe you're problem has to do with the polymorphism (although the 
syntax looks right to me and it is possible to eager load with a polymorphic 
association).

Did you try bullet?  https://github.com/flyerhzm/bullet

Really, try bullet, it will automatically tell you exactly the code to write to 
kills those N+1s. It's kind of amazing. 

-Jason



On Sep 20, 2014, at 9:01 PM, Qijun Feng <jun1st.f...@gmail.com> wrote:

> 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
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to rubyonrails-core+unsubscr...@googlegroups.com.
> To post to this group, send email to rubyonrails-core@googlegroups.com.
> Visit this group at http://groups.google.com/group/rubyonrails-core.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to