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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:52:30+00:00 2026-05-20T17:52:30+00:00

I have three models: class User < ActiveRecord::Base has_many :projects, :through => :permissions class

  • 0

I have three models:

class User < ActiveRecord::Base
 has_many :projects, :through => :permissions

class Permission < ActiveRecord::Base
 belongs_to :user
 belongs_to :project
 belongs_to :role

class Project < ActiveRecord::Base
 has_many :users, :through => :permissions

It’s very easy with the above to get all of a Project’s users: @project.users

But what I want to do is get something like this: Get all the Users in all of the user’s projects.

So if a user has 3 projects, each with 5 users. I want to query to get all 15 users across all of the user’s groups.

I’m trying that with.

 current_user.projects.users 

but Rails isn’t liking that much. current_user.projects works great, but not users.

Suggestions? Ideas? thanks!

UPDATED CODE 3 based on noodl’s comments

  scope :suggestedContacts, lambda { |user|
    users_from_projects = user.projects.reduce([]) {|all_users,prj|
      all_users + prj.users
    }.uniq
  }

ERRORS:

NoMethodError (undefined method `includes_values’ for #):

  • 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-20T17:52:30+00:00Added an answer on May 20, 2026 at 5:52 pm

    My two solutions are:

    • clean, simple standard rails meta-code, no custom finders
    • efficient, fetches in one SQL query, no N+1 issues
    • compliant, meaning you can still build, create etc. on the relation

    1.

    You can chain your relation in the user class.
    As rails 3.0.x does not yet support nested has_many_through you can use this plugin until rails 3.1

    class User < ActiveRecord::Base
     has_many :permissions
     has_many :projects, :through => :permissions
     has_many :users_in_projects, :through => :projects, :source => :user # chain the relation
    
    class Permission < ActiveRecord::Base
     belongs_to :user
     belongs_to :project
    
    class Project < ActiveRecord::Base
     has_many :users, :through => :permissions
    
    current_user.users_in_projects
    

    2.

    Another way would be to eager load and reduce (like the other answers have already described, but I’ll make it more explicit).
    This is more work, less dependencies.

    class User < ActiveRecord::Base
     has_many :permissions
     has_many :projects, :through => :permissions, :include => :users # eager load users
    
    class Permission < ActiveRecord::Base
     belongs_to :user
     belongs_to :project
    
    class Project < ActiveRecord::Base
     has_many :users, :through => :permissions
    
    current_user.projects.map(&:users).reduce(&:+).uniq_by(&:id) 
    # returns users in current_user's projects, one query, some computations
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have the following three models: User, Project, and Assignment. A User has_many Projects
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
I have 2 Models: # user.rb class User < ActiveRecord::Base has_one :profile, :dependent =>
Hi (huge Rails newbie here), I have the following models: class Shop < ActiveRecord::Base
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
In my Rails application, I have two models, Articles and Projects, which are both
I have a system where I need to login three user types: customers, companies,
I have a Rails 3 application with a User class, and a tab-delimited file

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.