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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:54:10+00:00 2026-06-17T01:54:10+00:00

I fully admit this is user inexperience but here is my problem I have

  • 0

I fully admit this is user inexperience but here is my problem

I have 2 models that relate by

     class User < ActiveRecord::Base
  belongs_to :team


  class Team < ActiveRecord::Base
  has_many :users

What I want to do is display all users that belong to the same team as the logged in user. so basically select from user there team_id = my team_id

I was not sure if I could / should do this in the controller or the view but I could not get either to work.

in my controller I have this which returns all users for all teams

 @users = User

which is SELECT users.* FROM users

I can also use my current_user method which returns info on my current user

 @users = current_user.team

which is SELECT teams.* FROM teams WHERE teams.id = 3

I dont know how to get a list of all users where team_id = current_user team_id?

Also would like to know if its best to try this in the controller or out in the view?

Thanks

  • 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-17T01:54:11+00:00Added an answer on June 17, 2026 at 1:54 am

    As suggested by @apneadiving using curent_user.team.users would return the list of users. But I just wanted to advise you to wrap that behavior in a method.
    So for instance you could have in your User model something like :

    class User < ActiveRecord::Base
    
      belongs_to :team
    
      def coworkers
        team.users
      end
    end
    

    And then in your controller you could do

    def an_action
      @coworkers = current_user.coworkers
    end
    

    Finally in your view you could loop through this list to display them

    @coworkers.each do |coworker|
      ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fully prepared to admit that I am missing something simple here. I
Please not that I fully understand this is a dumb ass idea, but its
I have this long query that I finally got to work but I am
I created MVC Application that have 3 different Area. (Admin, User, News) This is
I'm not sure if this belongs here or on super user. We have set
I fully appreciate the atomicity that the Threading.Interlocked class provides; I don't understand, though,
For my .net application i have a mechanism that creates a special user on
I have a small problem with an Xpage Application and hope that someone has
I have a fully functioning website with user accounts/profiles and admin area. I would
I have an app that I would like to create. But I am not

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.