Preload, Eager Load, Includes, and Joins in Ruby on Rails.

RailsCarma
3 min readSep 14, 2020

Let’s take an example to understand the difference between them.

One company has many employees, and one to many connections is there between the company and employees.

Preload:

Preload loads the data into two queries.

Includes also load associate data in two queries in the default case.

We can’t use preload to add conditions for the associate table. Like in our case we can’t add where a query for employees. It generates an error on adding where and order by with associated table.

eager_load:

Eager load loads all associated data in a single query. Railroad uses the left outer join to combine data. We can use where query also with eager load for filtering the data from the associated table.

Joins:

Joins loads data in a single query. It uses an inner join to bring data.

Joins is good if we are filtering the data from the associated table, But if we want to fetch data from a relational table, It loads a separate query for every column data.

Includes is a solution to prevent multiple queries. It loads the data from the associated table without multiple queries.

Includes as preload:

Includes as eager_load:

Eager_load loads all columns in a single query But Includes loads the data as per need.. It works as preload in default case and works as eager_load in case of the additional query in the associated table.

RailsCarma brings 10+ years of cutting-edge solutions expertise in creating Ruby on Rails Applications and enhancing your already existing Rails Applications. With a team of experts equipped with an extensive understanding of the RoR framework, RailsCarma helps you translate your business needs into quicker and cost-efficient development of tailored applications. By infusing agile practices into our RoR development, we take error-free coding to the next level with our exemplary post-deployment maintenance and support. Let us know about your business needs for us to help you realize your Rails Development goals with positive ROI.

--

--

RailsCarma
RailsCarma

Written by RailsCarma

Ruby on Rails Development Company

No responses yet