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
As suggested by @apneadiving using
curent_user.team.userswould 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
Usermodel something like :And then in your controller you could do
Finally in your view you could loop through this list to display them