Benito Serna Tips and tools for Ruby on Rails developers

Quiz to practice the difference between preload, includes or eager_load

February 22, 2021

Do you need more practice to understand the difference between “includes”, “preload”, “eager_load” and “joins”?

Here you will find a set of exercises to help you understand how you can use this 4 methods.

You can find the code on bhserna/preloading_quiz

This is are the exercises for the post Examples to learn the difference between preload, includes or eager_load, you can find the answers there.

On /exercises you will find a list of exercises with:

You should provide a way to fetch the records.

For example:

puts "-----------------------------"
puts "TASK"
puts "1. Fetch all acomodations preloading rooms (with includes)"
puts "-----------------------------"

# accomodations = ?
# display_with_rooms(accomodations)

How to run the examples

  1. Create a postgres database with createdb preloading_quiz. As you can see the in db/config.rb the name of the database is hardcoded, so you will need to create a database with that name.

  2. Install the dependencies with bundle install.

  3. Run the seeds with ruby db/seeds.rb. You can also update the data that you want to use, there.

  4. Run the exercises with ruby exercises/01.rb, etc…

Related articles

No more… “Why active record is ignoring my includes?”

Get for free the first part of the ebook Fix n+1 queries on Rails that will help you:

  • Explain what is an n+1 queries problem
  • Identify when ActiveRecord will execute a query
  • Solve the latest comment example
  • Detect n+1 queries by watching the logs
  • Learn the tools to detect n+1 queries
Get the first part of the ebook for free