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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:09:23+00:00 2026-06-03T12:09:23+00:00

I want to obtain Bar records associated to a single foo through bars in

  • 0

I want to obtain Bar records associated to a single foo through bars in the following models,
Foo <-> Bar <-> Baz.

class Foo < ActiveRecord::Base
  has_many :foo_bar_assocs
  has_many :bars, through: :foo_bar_assocs
end

class Bar < ActiveRecord::Base
  has_many :bar_baz_assocs
  has_many :bazs, through: :bar_baz_assocs
  attr_readonly :name
end

class Baz < ActiveRecord::Base
end

class FooBarAssoc < ActiveRecord::Base
  belongs_to :foo
  belongs_to :bar
end

class BarBazAssoc < ActiveRecord::Base
  belongs_to :bar
  belongs_to :baz
end

Naive implementation:

foo = Foo.find(id)
baz_of_foo = foo.bars.where(name: params[:name]).map{|b| b.bazs}.flatten.uniq

This code generates bad query, it executes many times.
What I want is something like the following:

baz_of_foo = foo.something_good_query(bar_name: params[:name])

Also, I want to get objects like ActiveRecord::Relation so that the resulting SQL queries are optimized.

baz_of_foo.each{ ... } # SELECT DISTINCT baz.* FROM ...
baz_of_foo.count       # SELECT COUNT(DISTINCT baz.*) FROM ...
baz_of_foo.exists?     # SELECT baz.* FROM ... TAKE 1
  • 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-03T12:09:24+00:00Added an answer on June 3, 2026 at 12:09 pm

    You should define the reverse relationships too, once you’ve done that you should be able to do something like this:

    Baz.joins(:bar => :foo).where(:bar => {:name => params[:name], :foo => {:id => foo_id}})
    

    For complex queries i would also recommend the squeel gem, it allows for more compact queries and also supports outer joins.

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

Sidebar

Related Questions

From the string [foo](bar) I want to extract the foo and the bar part:
I want to obtain the .text() of #inner2 <div class=outer id=outer> <div id=inner1 class=inner>test1</div>
Assuming the URL is http://localhost:8080/project-name/resource.xhtml , I want to obtain the following http://localhost:8080/project-name in
I want to write a script foo which simply calls bar with the exact
I want to obtain all the replaced values in the following exception from error
I want to obtain an array of ActiveRecord objects given an array of ids.
i want to obtain twitter follower user list that why i am using following
I want to obtain a running sum in query. I'm using the following running
I want to obtain the "base" property from a gtkstyle, which is a GdkColor
I want obtain full URL adres in php script, tell please, this code always

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.