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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:11:34+00:00 2026-05-22T21:11:34+00:00

Im very new to Ruby on Rails 3 and ActiveRecord and seem to have

  • 0

Im very new to Ruby on Rails 3 and ActiveRecord and seem to have been thrown in at the deep end at work. Im struggling to get to grips with querying data from multiple tables using joins.

A lot of the examples Ive seen either seem to be based on much simpler queries or use < rails 3 syntax.

Given that I know the business_unit_group_id and have the following associations how would I query a list of all related Items and ItemSellingPrices?

class BusinessUnitGroup < ActiveRecord::Base
  has_many :business_unit_group_items
end

class BusinessUnitGroupItem < ActiveRecord::Base
  belongs_to :business_unit_group
  belongs_to :item
  belongs_to :item_selling_price
end

class Item < ActiveRecord::Base
  has_many :business_unit_group_items
end

class ItemSellingPrice < ActiveRecord::Base
  has_many :business_unit_group_items
end

I’m confused as to whether I need to explicity specify any joins in the query since the associations are in place.

  • 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-22T21:11:34+00:00Added an answer on May 22, 2026 at 9:11 pm

    Basically, you do not need to specify the joins:

     # This gives you all the BusinessUnitGroupItems for that BusinessUnitGroup
     BusinessUnitGroup.find(id).business_unit_group_items
    
     # BusinessUnitGroupItem seems to be a rich join table so you might
     # be iterested in the items directly:
     class BusinessUnitGroup < ActiveRecord::Base
       has_many :items through => :business_unit_group_items
       # and/or
       has_many :item_selling_prices, through => :business_unit_group_items
       ...
     end
     # Then this gives you the items and prices for that BusinessUnitGroup:
     BusinessUnitGroup.find(id).items
     BusinessUnitGroup.find(id).item_selling_prices
    
     # If you want to iterate over all items and their prices within one 
     # BusinessUnitGroup, try this:
     group = BusinessUnitGroup.include(
       :business_unit_group_item => [:items, :item_selling_prices]
     ).find(id)
     # which preloads the items and item prices so while iterating,
     # no more database queries occur
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to Ruby on Rails, and have been getting the following error
I have built a very simple blog application using Ruby on Rails. New to
I'm fairly new in the Ruby + Rails scene. Although I have a very
I am very new to ruby / rails and have an issue that I
I'm very new to Ruby on Rails so please go easy! I've uploaded the
I am very new to Ruby and Rails. I use rails 3.0.9 and ruby
I am very new to ruby on rails. I've installed a complicated ruby on
i am very new to Ruby on rails . while running script/console , i
Hey, I'm very new to Ruby and Rails. I was wondering if I was
To begin, I am very new to Ruby on Rails. So I want to

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.