Ask A Question


You’re not receiving notifications from this thread.

devise_invitable: How to create a guest only after user confirmation by email

rodolfo asked in Gems / Libraries

I'm trying to create members of a team by using devise_invitable. But by looking the video related to devise_invitable, I'm not sure if I can create team members ONLY AFTER user confirmation by email. Any ideas?
Thank you


Hey rodolfo,

What problem are you trying to solve by creating the member only after the confirmation?


Hi @Jacob, thank you for your answer. I have 3 models: user, team and member. I'm trying to create a member by using devise_invited, but watching the devise_invited tutorial my app is currently creating a member of a team even if the invited user has not accepted the invitation by email. I wont to create a member only after the confirmation. Otherwise I will have members that do not exist as user. Here a fragment of the code:

Rails.application.routes.draw do
  resources :teams do
    resources :members
  devise_for :users
  root to: 'teams#index'
class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :invitable, :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatables
  has_many :own_teams, class_name: 'Team', foreign_key: :user_id
  has_many :members
  has_many :teams, through: :members
class Team < ApplicationRecord
  belongs_to :user
  has_many :members
  has_many :users, through: :members
class Member < ApplicationRecord
  belongs_to :user
  belongs_to :team
  attribute :email, :string
  before_validation :set_user_id, if: :email?
  def set_user_id
    self.user =  User.invite!(email: email)
class MembersController < ApplicationController
  before_action :authenticate_user!
  before_action :set_team
    def create
      member = = @team
        redirect_to @team, success: 'Bien hecho'
        redirect_to @team, alert: 'Mal hecho'
      def set_team
        @team = current_user.own_teams.find(params[:team_id])
      def member_params

Ok I see, you could put a status field on the member object so you can filter out members who haven't accepted their invitation and then change the status once the user has accepted the invite. This would allow you to periodically delete invitations that are over X days old so you don't end up with a bunch of abandoned records.

Another option would be to store the team ID on the invited user object so once the user accepts their invitation, you then create the member object since that really only functions as a join table between the user object and the team object.

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 79,047+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.