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 8599749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:27:48+00:00 2026-06-12T01:27:48+00:00

In my app, the main objects are Accounts and Phones, with a typical has_many

  • 0

In my app, the main objects are Accounts and Phones, with a typical has_many :through Contacts, eg:

Account:
has_many :contacts
has_many :phones, :though => contacts

Phone:
has_many :contacts
has_many :accounts, :though => :contacts

Contact:
belongs_to :account
belongs_to :phone

Contacts has fields signup_status, name
There is one Contact per unique Account/Phone pair

For an account with id = 123, which has 5 contacts, each contact having one phone, is there a query that would yield all 5 rows and include all the account fields AND contact fields AND phone fields?

  • 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-06-12T01:27:49+00:00Added an answer on June 12, 2026 at 1:27 am

    You can use eager loading of associations to get all the data you need in one active record query

    @account = Account.includes(:contacts, :phones).find(123)
    

    , which will actually translate into three SQL queries:

    SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = $1 LIMIT 1  [["id", 123]]
    SELECT "contacts".* FROM "contacts" WHERE "contacts"."account_id" IN (123)
    SELECT "phones".* FROM "phones" WHERE "phones"."id" IN (<phone ids found in prev query>)
    

    All of the records will be loaded into memory and become available through @account. To get the array of contacts and phones, just call @account.contacts and @account.phones, respectively. Note that these calls will not result in re-issued SQL queries, which is the beauty of eager loading.

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

Sidebar

Related Questions

My app's main view has a uiwebview. It is white for a split second
I am working on an app whose main window has an NSDrawer. It is
I've created an array of objects in the main app delegate that is loading
I have a main app and a type library contains 2 COM objects, one
My web app has 3 main node.js components: website, feeds and jobs. To start
I have a main app and a plugin. The main app is exporting an
I have a main app that will have many plugins. Here is the PluginReader
We have App A as main app. Now we build from it App B
I'd like to create another app from my main app, for instace 'app with
I have a widget that is a part of my main app. Usually when

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.