Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7195423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:34:35+00:00 2026-05-28T20:34:35+00:00

Is there a way to do a second query and have that replicate the

  • 0

Is there a way to do a second query and have that replicate the functionality of eager loading? Lets say a real basic example. A Person has many Cars and a Car has many Parts..

Typically to eager load you would say:

person.cars.includes(:parts)

I don’t really understand what this does.. does it just load those other objects in memory? Are they associated with each car at this point in a more substantial way then just sharing keys? If its just sharing keys.. could I load these ‘Parts’ in memory and have them accessed without calling the ‘includes’ explicitly? For example.. something like:

cars = person.cars
cars_id_array << insert cars ids here
parts = Parts.where(:user_id => person_id, "car_id IN cars_id_array") (just an example)

In this case (assuming i did the write code which I’m sure I didn’t.. could I go car1.parts, car2.parts and would those parts be already loaded in memory?

If not, what is eager loading doing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:34:36+00:00Added an answer on May 28, 2026 at 8:34 pm

    Eager loading is doing something very similar to what you are doing, but in addition to simply querying the database, it’s also establishing relationships. When you retrieve a model from the database that has related models, the related models are generally loaded on demand. The example from the guides is:

    clients = Client.limit(10)
    
    clients.each do |client|
      puts client.address.postcode
    end
    

    When you do the first query, there’s a call to the database to grab ten clients. As you iterate through them to get the postcode that is associated with a client address (which presumably lives in a different table), you’ll need to do 10 more queries to grab the proper addresses. By using eager loading, you can do the whole thing in just two queries (which are handled under the covers).

    clients = Client.includes(:address).limit(10)
    

    This grabs the ten clients, then does another query (similar to yours) to grab the addresses that are in the array of client_id’s. These are then resident in memory, with relationships in tact, so you don’t need to make another database call to get them.

    If you tried to replicate this by just loading them seperately:

    clients = Client.limit(10)
    addresses = Address.where("client_id in [my ID array]")
    clients.each do |client|
      puts client.address.postcode
    end
    

    You’re still going to be hitting the database for each of the addresses, since those objects aren’t associated with each other except through the database id’s. The objects are loaded, but the relationships aren’t created between them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way in .NET to have a class property have a second
Is there a way to load a table view that opens at the second
is there a way to write an sql query that finds all rows where
I have a query that needs to get the first and second highest sku
Is there a way to create a second Desktop screen in C# - just
Is there a way to avoid adding a second LEFT JOIN for the table
Is there any way of associating an object instance (object A) with a second
In Python is there any way to make a class, then make a second
I have a SQL query that I'm currently solving by doing two queries. I
I have an SQL query that executes a LEFT JOIN on another table, then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.