Reputation: 5619
I have the following models
class Client < ActiveRecord::Base
has_many :users, :dependent => :destroy
class User < ActiveRecord::Base
belongs_to :client
has_and_belongs_to_many :user_groups
class UserGroup < ActiveRecord::Base
has_and_belongs_to_many :users
has_and_belongs_to_many :courses
end
class UserGroupUser < ActiveRecord::Base
Now I want to display for one user to which user groups he belongs to.
I tried this:
@usergroups = UserGroup.joins(:users).where('user_groups.client_id IN (?)', [@current_client.id, session[:user_id]]).group('name').order('name')
But the result is anything, but not that what I expected. ...
The User Group Users looks like this:
I Want A list with all user_Groups for one User_ID how can I achieve this?
Upvotes: 0
Views: 34
Reputation: 991
Did you try:
User.find(user_id).try(:user_groups)
or if you have the User object available (an it's not null):
user.user_groups
Upvotes: 1