#30 Eager Loading with Goldiloader


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


Gemfilegem 'goldiloader'


      <% 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
  # 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

cavneb said 3 months ago:

Do you know if this work with Rails 5?

kobaltz PRO said 3 months 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