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

  • Home
  • SEARCH
  • 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 8278919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:09:36+00:00 2026-06-08T09:09:36+00:00

I have a parent / child relationship in my application class Polling has_many :alerts,

  • 0

I have a parent / child relationship in my application

class Polling
 has_many :alerts, :dependent => :destroy

class Alert
 belongs_to :polling

On my index page for the alerts, I need to show some data from each parent, and this results in two queries

Alert Load (6.1ms)  SELECT * FROM (SELECT * FROM "ALERTS" INNER JOIN "POLLINGS" ON "POLLINGS"."ID" = "ALERTS"."POLLING_ID" ORDER BY "ALERTS"."ID" DESC) WHERE ROWNUM <= 1
Polling Load (1.8ms)  SELECT "POLLINGS".* FROM "POLLINGS" WHERE "POLLINGS"."ID" = 10113 AND ROWNUM <= 1

Obviously, this makes the page loading time quite horrendous as it has to loop through each one and pull the parent object as well.

I have tried a few things, such as

> Alert.joins(:polling).where(...)
> Alert.includes(:polling).where(...)
> Alert.joins(:polling).select('*').where(...)

And each time I get two distinct queries when I visit my index page. One for each Alert, and then another to get its parent data. How can I do this on one line so that when I pull the alerts I also get their associated parent data? There doesn’t seem to be a way to go about it from the other end because if I do a Pollings.where(...) it doesn’t grab the children as a group.

  • 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-08T09:09:39+00:00Added an answer on June 8, 2026 at 9:09 am

    If you want to specify SQL in detail, you can always try find method passing :include, :joins, :where etc. includes or joins does same thing down the line when you try to access to data but behaviour of activerecord is different. For example, following 3 ways set up object array but they are different.

    [1]

    alerts = Alert.joins(:polling)
    

    [2]

    alerts = Alert.includes(:polling)
    

    [3]

    alerts = Alert.find(:all, :includes => :polling, :joins => :polling)
    

    when you do

    alerts.each do |alert|
      alert.pollings.each do |polling|
        p polling
      end
    end
    

    you can see how activerecord tries to get data from DB or cache. The [3] way, I believe, throws only one query when others throw multiple queries.

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

Sidebar

Related Questions

I have a parent-child relationship between two objects. Parent :has_many :children Child :belongs_to :parent
I have a parent/child relationship via our users table, with models as such: class
I have a parent-child relationship: @Entity @Table(name = user) public final class User {
Suppose I have objects that have a parent-child relationship like this: public class Node
I have parent/child relationship set up via Node Reference. A Child record can have
I have a parent-child relationship setup that is fairly basic. The end result is
Using parent child relationship where a parent can have children while each child has
I have a existing parent-child relationship I am trying to map in Fluent Nhibernate:
I have django model which consist of parent child relationship filed I want to
I have a Parent/Child object/mapping as follows: class Parent { int Id; string name;

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.