ruby on rails - Joining multiple relationships from separate tables into one association -
i have several models relevant question..
- usergrouppermission
- usergroup
- user
- userpermission
- permission
essentially, user can assigned permission through either usergroup or directly. usergroups tied specific permissions via usergrouppermission model. users tied specific permissions via userpermission model.
the end result able call user.permissions , return every permission assigned user either directly or through usergroup.
here current model
class user < activerecord::base belongs_to :user_group #permissions has_many :user_permissions has_many :user_group_permissions, :through => :user_group has_many :permissions, :through => :user_group_permissions #and :user_permissions end
i've search high , low solution this, can't seem find exact case. in advance!
edit: i've been told unclear i'm asking. help: user has_many :permissions, :through => :user_group_permissions , :user_permissions, can't use , join results together. i'm trying retrieve , combine results of both of relations one.
edit 2: i've been doing work. code gives desired result, i'm not sure how work potentially large databases.
#permissions has_many :user_permissions has_many :user_group_permissions, :through => :user_group #has_many :permissions, :through => :user_group_permissions #has_many :permissions, :through => :user_permissions def permissions all_permissions = [] usergrouppermission.where('user_group_id = ?', user_group_id).each |ugp| all_permissions.append(permission.find(ugp.permission_id)) end userpermission.where('user_id = ?', id).each |p| all_permissions.append(permission.find(p.permission_id)) end return all_permissions end
Comments
Post a Comment