Add a payload to an invitation system using ActiveRecord::SignedId

Hello!

I have a question / feature request for ActiveRecord::SignedId. I already built classic invitation system with it, the invitation logic looks like this:

# Generate the signed id and send it via email
Account.first.signed_id(expires_in: 1.week, purpose: :invitation)

I would now like to add a role to this invitation such as admin, manager, user. I don’t think there is a way to do this with signed ids yet. I would love to make a PR and add it to Rails if you find it interresting!

Would it make sense to do something like this:

# Generate an invitation to the account with the admin role
Account.first.signed_id(expires_in: 1.week, purpose: :invitation, payload: :admin)

# Retrieve the account and the payload
account, payload = Account.find_signed(
  Account.first.signed_id(expires_in: 1.week, purpose: :invitation, payload: :admin),
  extract_payload: true
)

The payload could be passed in the message something like “#{id}–#{payload}”. Does it make sense? Here I chose “–” as a delimiter between the id and the payload but it could be something else.

How about creating an Account Invitation model with a role column and send that with a signed id?

I’ve implemented invitations in the past and it’s always super useful to have a distinct table to store those. I obviously don’t know much about your domain but having a distinct model allows you to answer business questions like:

  • How many invitations are pending?
  • How many invitations have been accepted/rejected?

Or implement features like:

  • List of invitations
  • automatic reminders before invitations expire.
  • send manual reminders

Is there anything preventing you from just having a new model?

Hey Julian,

Thank you for your answer! I ended up createing an Invitation model with a role column. I was just wondering if that was possible because in my case, I didn’t need all the additionnal features you listed above like listing invitations, automatic reminders etc. so I wanted to do something very simple. But of course, creating an additionnal Invitation model works fine!

1 Like