Auto-include only after a certain number of calls to the association?
Closed this issue · 2 comments
Is there any way to configure Goldiloader to auto-include only when a certain number of calls to the associations are made?
For example, (not a great example), if we are iterating
latest_comment = nil
Blog.all.reorder(:id => :desc).each_with_index do |blog. index|
latest_comment = blog.comments.last if index == 0
#some other code to do something with each blog
end
In this example, we only called blog.comments once, and only for the first blog. So we didn't really need to auto_include all comments for every other blog.
But if we set a threshold, of say 3, and if we make a call to blog.comments for 3 other blogs, then have it do an auto_includes for every blog if we call blog.comments on a 4th blog.
Basically, IF we see X number calls to the association, THEN do the auto includes for the remaining associations in the collection.
Is something like this possible?
There's currently nothing to do this automatically but could you do it manually by enabling/disabling auto-include at the appropriate time? For example:
latest_comment = nil
Blog.all.reorder(:id => :desc).each_with_index do |blog. index|
latest_comment = blog.comments.auto_include(false).last if index == 0
#some other code to do something with each blog
end
This manual approach also ensures that you don't take the performance hit of running extra queries when you want regular automatic eager loading.
I came here looking for the same thing. I'd love to see a more integrated approach to this. Something like:
Blog.order(:name).auto_include(true, after: 3)