[RAILS] Verify the uniqueness of your email address

Let's distinguish users by other than user ID

It's a problem that one user can create any number of accounts. The operating cost of the DB, the quality of the user information collected, and these can be obstacles. The user ID is assigned to the user after the account is created. You need something other than a user ID that distinguishes you.

Let's distinguish by email address

It is impossible for a basic user to have the same email address more than once. Distinguish users by email address.

Limit user behavior

Specifically, you can not register using the same e-mail address that is already registered in the DB. Rails users can do this with the following features:

Let's limit with validation

Rails allows you to save data to a DB and refuse to save it if the data does not meet your requirements. You can intervene in user input. This function is called validation. Validation is done from data verification to refusal to save.

Let's set validation

Let's set the attributes to be validated for validation and what you want to validate for the attributes. The structure of the validation code is as follows.

Verify the uniqueness of your email address

What is uniqueness? It has the same meaning as only one. If you don't allow duplicate email addresses, users won't be able to easily have multiple accounts. Below is the source code.

validates :email, uniqueness: true

Now it's not possible to verify different email addresses for each user ... completely.

Ignore case in your email address

Letters are important in email addresses, regardless of case. Even if you change the lowercase email address to uppercase, the email will reach the same address. Ignore case in your email address.

Let's verify the uniqueness of the email address, ignoring the case

Below is the source code.

validates :email, uniqueness: { case_sensitive: false }

Case is ignored by setting the value corresponding to the case_sensitive option to true. Now it is possible to verify the uniqueness of the email address.

Recommended Posts

Verify the uniqueness of your email address
Check the domain by checking the MX record of the email address with java
Verify the contents of the argument object with Mockito
Improve the performance of your Docker development environment
The world of clara-rules (2)
Judgment of the calendar
The world of clara-rules (4)
The world of clara-rules (1)
The world of clara-rules (3)
The world of clara-rules (5)
The idea of quicksort
The idea of jQuery
Set the time zone in the JVM of your Azure application