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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:45:07+00:00 2026-06-11T23:45:07+00:00

A has many Bs, B has many Cs. C has a property called thing

  • 0

A has many Bs, B has many Cs. C has a property called thing:

class A < ActiveRecord::Base
  has_many :bs
end
class B < ActiveRecord::Base
  belongs_to :a
  has_many :cs
end
class C < ActiveRecord::Base
  belongs_to :b
  attr_accessible :thing
end

I’d like to query for all Bs belonging to an A, and eagerly load Cs that belong to said B:

> a = A.first
  A Load (0.2ms)  SELECT "as".* FROM "as" LIMIT 1
 => #<A id: 1, created_at: "2012-08-21 09:25:18", updated_at: "2012-08-21 09:25:18"> 
> bs = a.bs.includes(:cs)
  B Load (0.2ms)  SELECT "bs".* FROM "bs" WHERE "bs"."a_id" = 1
  C Load (0.1ms)  SELECT "cs".* FROM "cs" WHERE "cs"."b_id" IN (1)
 => [#<B id: 1, a_id: 1, created_at: "2012-08-21 09:25:22", updated_at: "2012-08-21 09:25:22", thing: nil>] 
> 

This works well:

> bs[0]
 => #<B id: 1, a_id: 1, created_at: "2012-08-21 09:25:22", updated_at: "2012-08-21 09:25:22", thing: nil> 
> bs[0].cs
 => [#<C id: 1, b_id: 1, thing: 2, created_at: "2012-08-21 09:29:31", updated_at: "2012-08-21 09:29:31">] 
> 

—but not in the case where I want to later perform where() searches on the Cs that belong to B instances:

> bs[0].cs.where(:thing => 1)
  C Load (0.2ms)  SELECT "cs".* FROM "cs" WHERE "cs"."b_id" = 1 AND "cs"."thing" = 1
 => [] 
> bs[0].cs.where(:thing => 2)
  C Load (0.2ms)  SELECT "cs".* FROM "cs" WHERE "cs"."b_id" = 1 AND "cs"."thing" = 2
 => [#<C id: 1, b_id: 1, thing: 2, created_at: "2012-08-21 09:29:31", updated_at: "2012-08-21 09:29:31">] 
> 

Note that queries are re-issued, despite our having the available information.

Of course, I can just use Enumerable#select:

> bs[0].cs.select {|c| c.thing == 2}
 => [#<C id: 1, b_id: 1, thing: 2, created_at: "2012-08-21 09:29:31", updated_at: "2012-08-21 09:29:31">] 
>

This avoids a re-query, but I was sort of hoping Rails could do something similar itself.

The real downside is that I want to use this code where we don’t know if the association has been eagerly loaded or not. If it hasn’t, then the select method will load all C for B before doing the filter, whereas the where method would produce SQL to get a smaller set of data.

I’m not convinced this matters at all, but if there was something I’m missing about eager loading, I’d love to hear it.

  • 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-11T23:45:09+00:00Added an answer on June 11, 2026 at 11:45 pm

    I don’t think you’re missing anything. I don’t believe active record can do anything that smart — and it would be very difficult to do reliably I think. Like you say, it would have to determine whether you’ve eager-loaded the association, but it would also have to make a guess as to whether it would be faster to loop through the in-memory collection of Cs (if it’s a small collection) or whether it would be faster to go to the database to get all the appropriate Cs in one shot (if it’s a very large collection).

    In your case, the best thing might be to just set the default scope to always preload the cs, and maybe even write your own fancy method to get them by thing. Something like this maybe:

    class B < ActiveRecord::Base
      belongs_to :a
      has_many :cs
      default_scope includes(:cs)
    
      def cs_by_thing(thing)
        cs.select{|c|c.thing == thing}
      end
    end
    

    Then you could always know that you never go back to the DB when querying for your cs:

    a = A.first
    [db access]
    a.bs.first
    [db access]
    a.bs.first.cs
    a.bs.first.cs_by_thing(1)
    a.bs.first.cs_by_thing(2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie question. I have the following models: class Asset < ActiveRecord::Base belongs_to :assetable, :polymorphic
There is one-2-many relation between Property and Reservation. Property has column called 'nr_of_bookings'. I
I am trying to save a record which has a many-to-one property mapping. I
I have a Type MenuItem that has one-to-many relationship with itself through Children property.
Mule has many endpoints, components, etc. for example like http:rest-service-component. How do I know
Okay so i got this clas called event. It has a property called eventDate
I have two EF entities. One has a property called HouseNumber. The other has
I have a Core Data object called Workshop. It has a to-many relationship to
My application has an Entity Framework model containing a many-to-many relationship like the following:
I have a class called Type: class Type { ... } Which has a

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.