On Wednesday, January 13, 2016 at 9:59:34 PM UTC, Flemming Thesbjerg wrote: > > > I am trying to optimize a slow query with an includes statement. > > > But a join table with a polymorphic association seem to be preventing it. > > > The following gist tries to illustrate the issue: > https://gist.github.com/flemse/fdd51ff5ad29a1f57134 > > > When running the code from the gist it will fail to load the join table > and therefore fail. > > > Any help would be greatly appreciated. >
I don't think includes can help you here. The preload strategy can't work here, because your a_comments association has a condition on the join table. The include (join based strategy) doesn't work because the association is polymorphic and you can't write a sql query that joins tables conditionally. It's possible that you could get this to run in 2 steps , if you put the condition on the post artefacts association and made a_comments go through that association. You might then be able to use includes to load the scoped post artefacts association and then invoke the preloader manually to load the comments (See https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/associations/preloader.rb ) Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-talk@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f915e352-a429-460f-8d6d-8e8285eb68e8%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.