#30 Eager Loading with Goldiloader

Summary

Wouldn't it be awesome if ActiveRecord didn't make you think about eager loading and it just did the "right" thing by default? With Goldiloader it can!
rails performance model 3:09

Summary

Gemfilegem 'goldiloader'

index.html.erb

      <% blog.tags.to_a.map(&:name).each do |tag| %>       <%# blog.tag_list.each do |tag| %>         <div class='badge'>           <%= tag %>         </div>       <% end %>

config/initializers/goldiloader.rbActiveRecord::Associations::Association.default_auto_include = false
# ActiveRecord::Associations::HasManyThroughAssociation.default_auto_include = false
blog.rbclass Blog < ActiveRecord::Base
  acts_as_taggable
  # If you set the initializer file to not auto_include, 
  # you can manually force an auto include
  # has_many :posts, auto_include: true
  
  # Since goldiloader is set to auto_include be default,
  # this behavior can be overridden.
  # has_many :posts, auto_include: false
end

cavneb said about 1 year ago:

Do you know if this work with Rails 5?

kobaltz PRO said about 1 year ago:

It does work with Rails 5! I've had luck with it, but you will want to make sure that you have pretty good test coverage to make sure stuff doesn't break. Goldiloader is pretty awesome, but it does add a bit too much 'magic'. The proper/better solution is to fix the performance gaps in the code rather than relying on the gem. However, if you do want to give it a shot, I've had luck with v0.0.12 and still getting passing tests. I think there was an issue on v2.0.0. It might have since been resolved.

Login to Comment