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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:01:03+00:00 2026-05-11T11:01:03+00:00

I’ve been stuck on this all day. I have a setup like the one

  • 0

I’ve been stuck on this all day. I have a setup like the one below. I’m trying to define friends using the group_memberships association.

class User < ActiveRecord::Base   has_many :group_memberships   has_many :groups, :through => :group_memberships   has_many :friends # what goes here? << end  class GroupMembership < ActiveRecord::Base   belongs_to :user   belongs_to :role   belongs_to :group end  class Role < ActiveRecord::Base   has_many :group_memberships end  class Group < ActiveRecord::Base   has_many :group_memberships   has_many :users, :through > :group_memberships end 

I’d like to do this without creating a join table for friends, unless it’s completely crazy to do it without.

The group_membership table contains user_id and group_id linking one user to one group.

I’d trying to get

@user.friends 

to return users with common group_memberships using the group_id.

has_many :friends, :through => :group_memberships, :source => :group 

Nothing I’ve tried works, but I’ll chalk that up to my complete misunderstanding of the above code.

  • 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. 2026-05-11T11:01:04+00:00Added an answer on May 11, 2026 at 11:01 am

    Unfortunately Rails doesn’t let you nest has_many’s more than 2 deep.. Forgetting about naming it friends for a moment (let’s call it users instead), this would theoretically be what you’d want:

    has_many :group_memberships has_many :groups, :through => :group_memberships has_many :users, :through => groups 

    Except that this doesn’t work. If you try it you’ll see this not-so-helpful error message which comes from this bit of code, specifically source_reflection.options[:through].nil?. That is, the through isn’t allowed to have a through itself.

    Instead, you may want to do something like this:

    Solution 1

    class User < ActiveRecord::Base   has_many :group_memberships   has_many :groups, :through => :group_memberships    def friends     groups.with_users.map(&:users).flatten.uniq.reject{|u| u == self}   end end  class Group < ActiveRecord::Base   has_many :group_memberships   has_many :users, :through => :group_memberships    named_scope :with_users, :include => :users end 

    Solution 2

    Use the nested_has_many_through plugin that Radar mentioned. It looks like at least one fork of it on github has been updated to work on the latest Rails.

    Solution 3 (just for kicks)

    or, just for kicks, you could do it with one big SQL query:

    class User < ActiveRecord::Base   has_many :group_memberships   has_many :groups, :through => :group_memberships    def friends     sql = <<-SQL       SELECT users.* FROM users, (         SELECT DISTINCT gm2.user_id AS user_id         FROM group_memberships gm, groups g, group_memberships gm2         WHERE gm.user_id = ? AND g.id = gm.group_id AND gm2.group_id = g.id AND gm2.user_id != ?       ) AS user_ids       WHERE users.id = user_ids.user_id     SQL     User.find_by_sql([sql, id, id])   end end 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.