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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:37:30+00:00 2026-06-19T03:37:30+00:00

Is there a way to easily and efficiently fetch the corresponding (child) models of

  • 0

Is there a way to easily and efficiently fetch the corresponding (child) models of a parent model and then render it in a template? I would like to know how to do it with and without joins

For Example, Consider these 3 tables:

# ProductGroup is the highest parent
class ProductGroup < ActiveRecord::Base
  attr_accessible :name, :merchant_id
  has_many :product_items
  has_many :product_group_selections
end

# ProductItem is a child of ProductGroup
class ProductItem < ActiveRecord::Base
  attr_accessible :base_price, :name, :product_group_id
  belongs_to :product_group
end

# ProductGroupSelection is a child of ProductGroup
class ProductGroupSelection < ActiveRecord::Base
  attr_accessible :name, :price_extra, :product_attr_group_id, :product_item_id
  belongs_to :product_group
  has_many :product_group_selection_attrs
end

# ProductGroupSelectionAttr is a child of ProductGroupSelection
class ProductGroupSelectionAttr < ActiveRecord::Base
  attr_accessible :name, :product_group_id
  belongs_to :product_group_selection
end

What I want is a data-structure that looks like this (when searching product_groups for merchant_id = 1)

merchant_id 1 => {  
          ProductGroup.name, ProductGroup.merchant_id,  
          ProductItems => [...],
          ProductGroupSelections => {ProductGroupSelections.name, ProductGroupSelectionAttrs => [...]}
}

This way I can loop through, in-turn, all groups and their sub models to generate a form using ERB.

Thank you

  • 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-19T03:37:32+00:00Added an answer on June 19, 2026 at 3:37 am

    When iterating over a collection of records that in turn have collections you’ll run into the infamous N+1 query. Essentially for every ProductGroup you would run a query to pull back all of it’s ProductItem records. And worse if your working with 2 levels of relations.

    To make this work more efficiently you want to make use of includes which ActiveRecord defines as a means of eager loading associations in as few queries as possible.

    ProductGroup.includes(:product_items).includes(:product_group_selections => :product_group_selection_attrs)
    

    From there you simply add on any conditions you need and whatever gets loaded for ProductGroup will ensure that all of the associated models also get loaded.

    Now you just iterate normally over your associations. Assuming @product_groups is has a collection of ProductGroup

    @product_groups.each do |product_group|
      # do stuff with product_group
      product_group.product_items.each do |product_item|
        # do stuff with product_item
      end
      product_group.product_group_selections do |product_group_selection|
        # do stuff with product_group_selection
        product_group_selection.product_group_selection_attrs do |product_group_selection_attr|
          # do stuff with product_group_selection_attr
        end
      end
    end
    

    The default way that rails sets up associations should fulfill the data structure you asked for, just with actual records instead of a hash of hashes, which you would need to load anyway to create the hash of hashes.

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

Sidebar

Related Questions

Is there a way to easily extract the json data portion in the body
Is there any way to easily make a HTTP request with C++? Specifically, I
Is there any way of easily deciphering (i.e. at a glance) whether or not
I can easily check IP addresses programmatically, but is there any way to set
I am wondering if there is a way that you can easily and safely
Is there way to automatically maximize the output window on hitting build and then
is there way how to get name ov event from Lambda expression like with
An array can be converted into a std::vector easily and efficiently: template <typename T,
Is there a way to easily/elegantly update multiple columns in a table with record
Is there a way to efficiently iterate over the values/items in a dictionary that

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.