Create a Devise User Model in Ruby on Rails
In this lesson we are going to create a user model to be stored in the database, and for this we are going to create a table that has all the user information in it.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this lesson we are going to create a user model to be stored in the database, and for this we are going to create a table that has all the user information in it.

To do that, we are going to type the command:

rails g Devise User

If you notice, we are not mentioning the word create anywhere because this is a special type of model that has a lot of devise methods already built-in to it. This command will generate the following files for you.

large

If you see, this command created a migrate file, a model, some tests and a route for us. Now, open the migration file 20151031163340_devise_create_users.rb.

large

Go through all this information because you may have to integrate many of these things in your application in the future. For example, you can use this information to tell the user when was the last time he or she logged in. Sometimes, you may want to send out an email to your dormant users to ask them to login and check out something on your website, and this information can help you add such functionality.

Another example is to check the number of failed attempts of a user, so that you can lock them out after a specified number of wrong logins. This can be particularly important if you're building secure applications like a banking system.

Now, go back to the terminal and do the migration with this code,

rake db:migrate

and this would create our table.

large

To check, go to the schema file and you can see a table called "User" with all the fields we discussed earlier.

large