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.
It is impossible for a basic user to have the same email address more than once. Distinguish users by email address.
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:
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 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.
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.
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.
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