Felix
Felix

Reputation: 5619

Rails show specific results from database

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: enter image description here

I Want A list with all user_Groups for one User_ID how can I achieve this?

Upvotes: 0

Views: 34

Answers (1)

Felipe Arenales
Felipe Arenales

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

Related Questions