samedi 25 avril 2015

Joining multiple relationships from separate tables into one association


I have several models that are relevant to this question..

  • UserGroupPermission
  • UserGroup
  • User
  • UserPermission
  • Permission

Essentially, A User can be assigned a permission through either their UserGroup or directly. UserGroups are tied to specific permissions via the UserGroupPermission model. Users are tied to specific permissions via the UserPermission model.

The end result is to be able to call User.permissions and return every permission assigned to that user either directly or through a UserGroup.

Here is my 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 and low for a solution to this, but can't seem to find my exact case. Thanks in advance!

Edit: I've been told that is unclear what I'm asking. Hopefully this will help: Basically the User has_many :permissions, :through => :user_group_permissions and :user_permissions, but you can't use an and to join the results together. I'm trying to retrieve and combine the results of both of those relations into one.


Aucun commentaire:

Enregistrer un commentaire