ruby - Rails join, need both tables in result -


i have 2 tables users (id, name ...) , user_group_users (user_id, usergroup_id)

i try join on both tables, need in result

user.id user.name user_group_users.user_id, user_group_users.user_group_id

tried this:

@users = current_client.users.joins('left inner join user_group_users on user_group_users.user_id = users.id') 

but in result user_group_users.user_id, user_group_users.user_group_id missing

how can solve that?

models:

class usergroupuser < activerecord::base   belongs_to :user_group   belongs_to :user end  class usergroup < activerecord::base   has_many :user_group_users   has_many :users, :through => :user_group_users end   class user < activerecord::base   has_many :user_groups   has_many :user_group_users, :through => :user_group_users end 

i want generate list, users: enter image description here

and need user_id , usergroup_id usergroupusers set switcher on if user in group.

you're trying , not letting rails work it's intended you. should read documentation in rails carefully, because of information need there, , can find several examples online using google.

in case, need "has , belongs many" relation. described in documentation, similar "has many... through" doesn't require code know intermediate model (what call, usergroupuser).

class usergroup < activerecord::base   has_and_belongs_to_many :users end  class user < activerecord::base   has_and_belongs_to_many :user_groups end 

in code, let's suppose have usergroup active record object in @group , have user active record object in @user. tell if user in group:

@group.users.map { |u| u.id }.include? @user.id 

if want check every user, loop user.all.each |user| ....

you can rid of usergroupuser model, still need table, described in documentation.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -