I am developing a RoR application in which users can make Lists and invite other users to join their list(s). I am not sure how I should approach the invitation data model. I would assume that a user invitation model would be fairly common but I have not found any good examples.
I’d like any user to be able to invite any other user to their list. The invitee (an another user) would then be able to accept or reject the invitation.
I have a many-to-many relationship between users and lists currently. Should the join table between these two entities (lists_users) hold information concerning invitations? Would it be better to create another table, invitations, and add and remove records from it as users accept or reject invitations? If the latter is advisable how should I handle list owner (user) vs. list invitees (user)?
I’m grateful for any advice. Thanks!
Solution
I found it necessary to specify the table containing the foregin keys in User Model. ie
has_many :owned_lists, :class_name => "List", :foreign_key =>"owner_id"
has_many :created_invitations, :class_name => "Invitation", :foreign_key =>"invitor_id"
has_many :received_invitations,:class_name => "Invitation", :foreign_key => "invitee_id"
-Nick
These are just my own two cents, but here’s how I’d do it.
I’d keep one table
lists_usersto keep track of the users that have joined a list.I’d have a
user_idcolumn in theliststable to keep track of owners of lists.I’d then have a ’Invitation’ model with, for example,
invitee_id,inviter_idandlist_id, to keep track of invitations. An invitation is destroyed when it is used. Invitations probably also need some kind of active-until date.The models would look like this: