JT Book a call

Blog

What is the difference between joins and includes in Rails ActiveRecord?

Published

1 min read

What is the difference between joins and includes in Rails ActiveRecord?

The main difference is that when using includes the eager loading is used. Eager loading allows you to optimize data retrieval by avoiding additional queries to the database.

Let’s see how it works with an example:

Joins: Image description

We have joined tables, but we don’t get any data from the users table. Therefore, each time the user record is accessed, and there is an additional query to the database.

Includes: Image description

As you can see, we don’t have any additional queries because we use eager loading and we get the data about the user from memory instead of querying the database.

Read next

Tell us whatyou need built

JetThoughts has been building software since 2008. Book a call, or write to sales@jetthoughts.com. You'll leave the call with a written one-page scope within 2 working days.

#01 Paul Keen
Paul KeenFounder

A 30-minute call; pick a time.