Reputation: 20445
I have a class User, which has many Events and belongs to many Groups. I want to allow user to create Event and restrict who he wants the invite by Group, by user id or by both.
How should I allow for multiple select box tag that shows both groups and individuals as selectable? I am thinking of creating something like acts_as, but it seems to be unnecessarily complicated.
Upvotes: 0
Views: 522
Reputation: 4143
WARNING!!! First Iteration solution - it could be simpler
You might need a polymorphic relation to achieve the mapping.
Event
has_many :invitations
has_many :user_invitations, :class_name => "Invitation", :conditions => "inviteable_type = 'User'"
has_many :group_invitations, :class_name => "Invitation", :conditions => "inviteable_type = 'Group'"
Invitation
belongs_to :inviteable, :polymorphic => true
#Invitations table would have inviteable_id, inviteable_type columns
User
has_many :invitations, :as => :inviteable
Group
has_many :invitations, :as => :inviteable
Now you can have `Event.first.inviteables (this will return a collection of Users and Groups)
In your view, name the options for your 2 collections differently, example:
<select name='event[event_inviteables]' ...>
<option value='group[][1]'>Group 1</option>
..
<option value='user[][1]'>User 1</option>
In your Event model, you'll need to have an event_inviteables=
method to determine which ones are users, and which ones are groups and update the inviteable collections accordingly
Event
def event_inviteables=(*args)
user_ids = #some magic to extract user_ids from args
group_ids = #some magic to extract group_ids from args
self.user_invitation_ids = user_ids
self.group_invitation_ids = group_ids
end
Upvotes: 1