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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:25:47+00:00 2026-05-23T06:25:47+00:00

I have a Partner model that has_and_belongs_to_many Projects, while each Project has_many Sites. I

  • 0

I have a Partner model that has_and_belongs_to_many Projects, while each Project has_many Sites. I want to retrieve all sites for a given partner (and am not interested in the projects in between at the moment).

I have accomplished what I need through a named_scope on the Site model, and a project.sites instance method that wraps a call to the Site named scope, as follows:

class Partner < ActiveRecord::Base

  has_and_belongs_to_many :projects

  def sites
    Site.for_partner_id(self.id)
  end

end


class Project < ActiveRecord::Base

  has_many :sites

end


class Site < ActiveRecord::Base

  belongs_to :project

  named_scope :for_partner_id, lambda {|partner_id|
    { :include=>{:project=>:partners},
      :conditions=>"partners.id = #{partner_id}"
    }
  }

end

Now, given a partner instance, I can call partner.sites and get back a collection of all sites associated with the partner. This is precisely the behavior I want, but I’m wondering if there’s another way to do this using only activerecord associations, without the named scope?

  • 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-23T06:25:48+00:00Added an answer on May 23, 2026 at 6:25 am

    I had a similar deep nesting query/collection problem here (I had to threaten to repeat data before anyone would answer my 4 questions, clever):
    Is it appropriate to repeat data in models to satisfy using law of demeter in collections?

    The trick is this gem http://rubygems.org/gems/nested_has_many_through which can do something like this:

    class Author < User
      has_many :posts
      has_many :categories, :through => :posts, :uniq => true
      has_many :similar_posts, :through => :categories, :source => :posts
      has_many :similar_authors, :through => :similar_posts, :source => :author, :uniq => true
      has_many :posts_of_similar_authors, :through => :similar_authors, :source => :posts, :uniq => true
      has_many :commenters, :through => :posts, :uniq => true
    end
    
    class Post < ActiveRecord::Base
      belongs_to :author
      belongs_to :category
      has_many :comments
      has_many :commenters, :through => :comments, :source => :user, :uniq => true
    end
    

    This has super-simplified my queries and collections. I hope you find an answer to your problem, it’s a tough one!

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

Sidebar

Related Questions

I am having that problem that my model dont want to save. I have
My issue is following: My Partner's have websites and I want that partners can
I have a class called Partner, that I need to Submit all the info
My partner and I have a webcam site that basically runs the old-school method....Every
I have a static variable partner in the class. And I want to set
I have some code from a Chinese business partner, and all of the comments
Have a class definition that looks something like this class someClass(models.Model): field1 = models.ForeignKey('other_app.field4')
We have a partner that is actually owning our app on it's iTunes Connect
We have a business partner that is IFRAMEing some data to one of our
Unfortunately Amazon.com/.de do have separated partner-programs, so that there is no ready to use

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.