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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:03:18+00:00 2026-05-28T01:03:18+00:00

I have 4 models, A, B, C and D class A < ActiveRecord::Base has_many

  • 0

I have 4 models, A, B, C and D

class A < ActiveRecord::Base
  has_many :B
  has_many :C, :through => :B
end  

class B < ActiveRecord::Base
  belongs_to :A  
  has_many   :C
  has_many   :D, :through => :C
end  

class C < ActiveRecord::Base    
  belongs_to :B
end    

class D < ActiveRecord::Base    
  belongs_to :C
end    

I have a very naive implementation which is very obvious …

<% A.B.each do |b| %>
  <%= b.number %>
  <% b.C.each do |c| %>
    <%= c.name %>
  <% end %>
<% end %>

What’s the best way to get All C for A?
What’s the best way get All D for A?

I want to get all ‘C’ using order_by clause with “created_at” value instead of iterating through B.

May be I’m missing some ActiveRecord magic?

I appreciate any help.

  • 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-28T01:03:18+00:00Added an answer on May 28, 2026 at 1:03 am

    First of all, you need to make a couple changes.

    1. class C needs an association to D

      class C < ActiveRecord::Base
        belongs_to :B
        has_one :D
      end
      
    2. If you want to access A‘s D‘s, you need to specify this as well.

      class A < ActiveRecord::Base
        has_many :B
        has_many :C, :through => :B
        has_many :D, :through => :C
      end
      

    Now, to access all of A‘s C‘s:

    -> a = A.where(:id => 1).includes(:C).first
      A Load (0.2ms)  SELECT "as".* FROM "as" WHERE "as"."id" = 1 LIMIT 1
      B Load (0.1ms)  SELECT "bs".* FROM "bs" WHERE "bs"."a_id" IN (1)
      C Load (0.1ms)  SELECT "cs".* FROM "cs" WHERE "cs"."b_id" IN (1, 2)
     => #<A id: 1, created_at: "2012-01-10 04:28:42", updated_at: "2012-01-10 04:28:42"> 
    -> a.C
     => [#<C id: 1, b_id: 1, created_at: "2012-01-10 04:30:10", updated_at: "2012-01-10 04:30:10">, #<C id: 2, b_id: 1, created_at: "2012-01-10 04:30:11", updated_at: "2012-01-10 04:30:11">, #<C id: 3, b_id: 2, created_at: "2012-01-10 04:30:21", updated_at: "2012-01-10 04:30:21">, #<C id: 4, b_id: 2, created_at: "2012-01-10 04:30:21", updated_at: "2012-01-10 04:30:21">]
    

    Notice how another query is not executed when you call a.C. This is because ActiveRecord knows you will want to access the found A‘s C‘s by the include call, and generates the minimum number of queries. Same goes for D‘s:

    -> a = A.where(:id => 1).includes(:D).first
      A Load (0.1ms)  SELECT "as".* FROM "as" WHERE "as"."id" = 1 LIMIT 1
      B Load (0.1ms)  SELECT "bs".* FROM "bs" WHERE "bs"."a_id" IN (1)
      C Load (0.1ms)  SELECT "cs".* FROM "cs" WHERE "cs"."b_id" IN (1, 2)
      D Load (0.1ms)  SELECT "ds".* FROM "ds" WHERE "ds"."c_id" IN (1, 2, 3, 4)
    

    Say you wanted all A‘s D‘s but wanted C‘s ordered:

    A.where(:id => 1).includes(:C).order('cs.created_at DESC').includes(:D)
    

    Note you can also set this as a default on the association:

    The :order option dictates the order in which associated objects will be received (in the syntax used by an SQL ORDER BY clause).

    class Customer < ActiveRecord::Base
      has_many :orders, :order => "date_confirmed DESC"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have 2 models: Video: class Video < ActiveRecord::Base belongs_to :user has_many :thumbnails attr_accessor
I have these 3 models: class Car < ActiveRecord::Base belongs_to :user has_many :car_styles has_many
I have the following models class Project < ActiveRecord::Base has_many :project_members has_many :members, :through
My models class Team < ActiveRecord::Base has_many :team_players has_many :players, :through => :team_players end
I have three models class Collection < ActiveRecord::Base has_many :presentations has_many :galleries, :through =>
I have the following models: class Label < ActiveRecord::Base has_many :releases end class Release

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.