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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:54:16+00:00 2026-05-25T02:54:16+00:00

I have a model Foo . Foo has many children of type FooChild .

  • 0

I have a model Foo. Foo has many children of type FooChild. FooChild has many FooChildChild. Foo, FooChild and FooChildChild all have and belong to many Bar. I want to create a method or scope on Foo that will return all of the Bar associated with it and it’s child associations. I currently have something like the following on the Foo model.

def foo_child_bars
    b = []
    self.foo_childs.includes(:bar).collect{|fc| b += fc.bar}
    b.uniq
end

def foo_child_child_bars
    b = []
    self.foo_childs.includes(:foo_child_childs).collect{|fcc| b += fcc.bar}
    b.uniq
end

def all_bars
    (self.bars + self.foo_child_bars + self.foo_child_child_bars).uniq
end

So now I can call @foo.all_bars and this works and I don’t get duplicates because of the .uniq method.

I imagine that this sort of operation is rather inefficient due to the fact that I’m performing three separate queries. Ideally I’d like to do a single query with a UNION or something so that the database does the heavy lifting of ensuring only unique records are returned and I’m doing as few queries as possible.

I’m also curious if there is a way to use includes() on a nested association such that foo_child_child_bars() could include the :bars which are associated with all of the :foo_child_childs which are being included.

Bar in this reduction represents a table 'images' which has CarrierWave set up on the Image model for managing uploaded images. I’m almost positive that there is a gem out there that would probably simplify this whole thing, but I’d rather just make what I have work and tackle that gem implementation later as it will likely involve a lot of complicated table migrations and refactoring.

  • 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-25T02:54:17+00:00Added an answer on May 25, 2026 at 2:54 am

    two queries

    is Bar polymorphic?

    ids = Foo.includes(:foo_children => :foo_child_children).map{|foo| [foo.id, foo.foo_children.map(&:id), foo.foo_children.map(&:foo_child_child).flatten.compact.map(:&id)]}.flatten.compact
    Bar.where(:obj_id => ids)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have model Foo which has field bar. The bar field should be unique,
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
I have an ActiveRecord model class Foo that has_many Bar . I want to
Let's say I have a model foo, and my model has a publish! method
I have a model with nested attributes : class Foo < ActiveRecord::Base has_many :bar
I have an ActiveRecord model, Foo , which has a name field. I'd like
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
I have this: SolutionName: Foo.sln Assembly: Foo.Bar Namespaces are: Foo.Bar.Views Foo.Bar.Model Foo.Bar.BusinessObjects Foo.Bar.Services Should
I have a model Foo that has_many 'Bar'. I have a factory_girl factory for
I have a model, Foo, that has_many Bars. It has a virtual attribute, current_baz,

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.