I am trying to build an invitation system for my app and I feel like there's a nice way to do what I need, yet I'm not sure about it.
I have a User model and I created a UserInvite model.
When an existing user sends out a new invite, a UserInvite is created. I am storing when the invite was sent, generate an invite token to make signing up easier and send an email to the invited user.
When the invite is accepted, I would also like to store the user id of the new user on the UserInvite object.
That is, a UserInvite belongs to both inviter and the invitee, and the latter is only being added to the object later on, not at creation, since I don't know the id of the to-be-created User.
What is the right way to model this relationship?
I'm using Rails 7.
If you look at Devise::Invitable as a reference then the answer is You Ain't Gonna Need It. It just adds columns to the user model to track invitations:
These columns are used to bypass several validations so that you can create a user record without a password - which is then "claimed" by updating the record with a password.
If you want to do a separate table for invitations then there isn't really a clear need to store the invited users id either unless you actually plan on displaying or using the invitations in some way.
The invitation will simply be used as a token when signing the user up and can then be considered spent and deletable. If you want to record who invited the user I would just add a
inviter_idto the users table:If you really want to go down the path of two columns pointing to the users table then you can do it with something like:
But again - YAGNI. This will add additional complexity and a UPDATE query.