Eager Loading with Goldiloader

Episode #30 by Teacher's Avatar David Kimura

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

Resources

Summary

# Gemfile
gem '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.rb
ActiveRecord::Associations::Association.default_auto_include = false
# ActiveRecord::Associations::HasManyThroughAssociation.default_auto_include = false

# blog.rb
class 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