2

I was wondering if someone could help me model the below scenario. I have a User class, an Event class and a Venue class in Mongoid. I would like for users to be admins of either an Event or a Venue or both. These different roles could end up spiraling out of control and I was wondering if this would be an good case for polymorphism.

Venue

has_and_belongs_to_many :admins,    :class_name=> 'User', :inverse_of=>:venue_adminships
has_and_belongs_to_many :managers,  :class_name=> 'User', :inverse_of=>:venue_managementships

Event

has_and_belongs_to_many :admins,   :class_name=> 'User', :inverse_of=>:event_adminships
has_and_belongs_to_many :managers,  :class_name=> 'User', :inverse_of=>:event_managementships

User

has_and_belongs_to_many :venue_adminships,  :class_name=>"Venue",  inverse_of: :admins
has_and_belongs_to_many :venue_managementships, :class_name=>"Venue",  inverse_of: :managers

has_and_belongs_to_many :event_adminships,  :class_name=>"Event",  inverse_of: :admins
has_and_belongs_to_many :event_managementships, :class_name=>"Event",  inverse_of: :managers

I need to be able to pull up a User's "adminships" (while I'm at it, anyone have a better name than "adminship".... ? ) as well as pull up all the admins for an Event. What would be a cleaner way to represent this without so many repeating relationships?

Community
  • 1
  • 1
jdkealy
  • 4,807
  • 6
  • 34
  • 56

2 Answers2

0

Mongoid documentation:

Polymorphic behavior is allowed on all relations with the exception of has_and_belongs_to_many.

http://mongoid.org/en/mongoid/docs/relations.html

So, you can't use polymorphic for this problem.

rjurado01
  • 5,337
  • 3
  • 36
  • 45
0

Use inheritance, so both venue and event will be stored in the same collection.

Create a class Administrable or something like that, and then Venue and Event inherit this class.

The documents will all be stored in the collection Administrable.collection_name, which should be 'administrables'.

The user has and belongs to many administrables, you just have to check the type to display the correct view.

Jaffa
  • 12,442
  • 4
  • 49
  • 101