Generating the User Model

Generate the User model of the application.

We'll cover the following

In the last chapter, we managed to set up the bare bones for our application’s endpoints configuration.

In the next chapter, we will handle user authentication through authentication tokens and set permissions to limit access for, let’s say, signed-in users.

In the coming chapters, we will relate products to users and give them the ability to place orders.

There are many authentication solutions for Rails, including AuthLogic, Clearance, and Devise. These solutions are turnkey libraries, which allow us to manage things like authentication, password forgetfulness, validation, and more. Nevertheless, we will use the bcrypt gem to hash the user’s password.

Note: This chapter is on the longer side and we will cover many topics. Feel free to have a coffee, stretch, or prepare however you like. By the end of this chapter, we will have built all the user logic, validation, and error management.

The User model

A model is a Ruby class that is used to represent data. The model contains the data and the logic related to it, including validation, reading, and recording. We will start by generating our User model. This model will be straightforward and will contain only two fields:

  • The email field should be unique.
  • The password_digest field contains the hashed version of the password (we will discuss this later in this chapter).

Get hands-on with 1400+ tech skills courses.