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

  • Home
  • SEARCH
  • 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 8178383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:45:10+00:00 2026-06-06T23:45:10+00:00

I have a user system in which a user can be associated with a

  • 0

I have a user system in which a user can be associated with a group. This works fine.

Now I am in the need to be able to associate a user with a group and that group can be associated with another group. I want to be able to fetch the users that belongs to both the parent group and all the child groups in one operation.

So it goes:

Group -> Group -> Member

This is how the setup looks today:

class Group < ActiveRecord::Base

  belongs_to :customer

  has_many    :child_groups, class_name: "Group", foreign_key: 'parent_group_id'
  belongs_to  :parent_group, class_name: "Group"

  has_many :memberships, :class_name => "Group::Membership"
  has_many :members, :through => :memberships
end

And Group::Membership looks like this:

class Group::Membership < ActiveRecord::Base

  belongs_to :member
  belongs_to :group
  has_many :customers, :through => :group

end

Lets say I fetch a group which has several child groups associated with it with the command Group.first.members and fetch all the members in both the main group and all the members in the child groups in preferably one DB request.

Is this possible with only base Rails associations or do I need to do some custom SQL?

  • 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-06T23:45:11+00:00Added an answer on June 6, 2026 at 11:45 pm

    I would look into using Single Table Inheritance. Have all your other groups inherit from the parent group.

    class ParentGroup < ActiveRecord::Base; 
     belongs_to :user
    end
    
    class Group < ParentGroup; end
    

    You will then be able to query ParentGroup.all and see every group.

    search for Single Table Inheritance in this link
    http://api.rubyonrails.org/classes/ActiveRecord/Base.html

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

Sidebar

Related Questions

I have make a messaging system in which user can send messages to each
I have a system whereby a user can view categories that they've subscribed to
I have users in my system that can elect to 'hibernate', at which point
okay, i'm setting up a multi-user chat system. i have a messages table, that
I'm setting up a group / user based security system. I have 4 tables
I have a database which is part of a closed system and the end-user
I am presently looking for a user management system in PHP which can easily
I have a user system set up in a 'users' table, and I have
I have an admin system and a user system on my website. Some data
I am going to be making a small user system but I have questions.

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.