Here is the organized article:
Adding Workspaces
The previous article on adding sign ups left the option to add a Workspace on sign up. Let’s add that now. It’s simple really. All it is, for now, is a record with a has_many :users. All business records your users create will belong to the workspace, giving the users access to them (but that will be for another article).
Creating the Workspace Model
Let’s create the model first: rails g model Workspace name:string. Then update the existing User model by adding a workspace_id: rails g migration add_workspace_id_to_users workspace_id:integer. Update the new Workspace model like this:
# app/models/workspace.rb
class Workspace < ApplicationRecord
has_many :users
end
Adding Invites
What I am having in mind is the following:
- Workspace "owner" (the User would created the Workspace) adds email from invitee;
- Invitation model is created, with: email and inviter_id;
- After Invitation create, an email is sent to the email with an invite link;
- On clicking the link, invitee sees a form with email and password;
- Upon submit, an User model is created and attached to the Workspace.
Creating the Invitation Model
Let’s create the invitation model first: rails g model Invitation workspace_id:integer inviter_id:integer email_address:string accepted_at:datetime. Update the… root_path to include the accepts_invitation action:
# config/routes.rb
Rails.application.routes.draw do
#...
resources :accept_invitations, only: %w[new create]
end
# app/views/accept_invitations/new.html.erb
<%= form_with model: @invitation, url: accept_invitations_path do |form| %>
<%= form.email_field :email_address %>
<%= form.password_field :password %>
<%= form.submit %>
<% end %>
Creating the AcceptInvitation Class
Let’s create the AcceptInvitation class:
class AcceptInvitation
#...
private
def update_invitation
@invitation.update accepted_at: Time.current
end
def add_new user, to:
to.users << user
end
def invitation = Invitation.find_by_token_for invitation, token
end
Conclusion
This class is the place to handle everything needed after the invite is accepted. I’ve left it to updating the accepted_at column (remember how that expires the token) and adding the new user to the workspace. But you can add any action that makes sense for your business logic.
FAQs
Q: What is a Workspace?
A: A Workspace is a place where users can create and access business records, such as companies, teams, or projects.
Q: Why do I need to add Workspaces?
A: Adding Workspaces allows your users to have a place to store and organize their business records, making it easier to work with others and track progress.
Q: Can I customize the Invite process?
A: Yes, you can customize the invite process to fit your business logic. For example, you could add additional validation or send notifications to the workspace owner when a new user joins.

