Making your Rails web app invite only using devise_invitable

There are many sites and applications that begin their lives as "invite only." This can be a very effective method of creating a sense of scarcity and at the same time build in a certain amount of curation to your user base. A good current example of a site that is still invite only is Dribble, a site for designers to share their work. The QA site Quora began as an invite only application but recently opened its doors to all comers.

In the Ruby on Rails world the current User authorization hotness is Devise, a gem created by José Valim of Plataformatec (see his great new book here: Crafting Rails Applications). Devise has a great community around it and is built to be modular, only use what you need. One of the pieces that it does not include is a method for sending invitations to new users and this is where the gem devise_invitable by Sergio Cambra comes in with the save.

One of the features that is really useful in an invite only world is to set how many invitations a User is allowed to send. In the Readme on github devise_invitable says that it accomplishes this with the invitation_limit field, the only problem with this piece of documentation is that it refers to something that is not in the current stable release of the devise_invitable gem, which is v0.3.6. To get the added capability of using invitation_limit, invited_by_id, validate_on_invite and invite_key you need to use at least v0.4.rc5. This release candidate currently relies on Devise v1.2.0 so your Gemfile should include the following:

If you are installing Devise for the first time then you should follow the instructions in the Readme here to get it setup, but before you run "rake db:migrate" you should add the line t.invitable to the ######_devise_create_users.rb migration. If you are adding devise_invitable to an app that already has Devise configured then you will need to create a migration that will add the missing fields to your Model. This migration would look like this:

Before you run this migration you should know how the invitation_limit is set in case you decide to handle things differently. To set the default number of invitations that a User can send you will uncomment/add the line config.invitation_limit = 5 to the initializers/devise.rb file (don't forget to restart your server when in development to apply changes to config). The only drawback to this method by itself is that the number of invitations a User has is not set until they send their first invitation. This means that if you want to display how many invitations a User has left before they send their first one by referencing current_user.invitation_limit it will not display anything until after they have sent their first invitation at which point this would now be equal to 4. If you want to avoid this inconvenience you can change the line in the migration to add a default number for the invitation_limit when a user is created.

t.integer :invitation_limit, :default => 5

If you use this method then you still need to uncomment/add the config.invitation_limit line to your devise.rb initializer but it does not matter what number you set it to as long as it is greater than zero.