This time I would like to summarize what I learned after completing Chapter 6 of the Rails Tutorial. Since we created the User controller in Chapter 5, we will create the User model in Chapter 6. I will check it immediately.
The basics of the user model data structure.
users | |
---|---|
id | integer |
name | string |
string | |
created_at | datetime |
updated_at | datetime |
First, create a User model with the following command.
$ rails generate model User name:string email:string
It is important to keep in mind the convention of using the plural for controller names and the singular for model names. The controller is Users and the model is User. Let's take a look at the migration file generated by the command.
db/migrate/[timestamp]_create_users.rb
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps
end
end
end
Migration refers to "a collection of change methods that define the changes you make to a database." The model name is singular, but the table is plural. The last t.timestamps in the migration file creates a magic column. When applied to the user model, it automatically records the time when a user is created or updated.
If there is no problem with the migration file, create a database with the following command.
$ rails db:migrate
Executing the above command will create a User table in the database and update schema.rb. The above is a series of flow until the User model is created.
In Chapter 6 of the Rails tutorial, we will implement presence, length, format, uniqueness, and confirmation.
presence
app/models/user.rb
class User < ApplicationRecord
validates :name, presence: true
validates :email, presence: true
end
length
app/models/user.rb
class User < ApplicationRecord
validates :name, presence: true, length: { maximum: 50 }
validates :email, presence: true, length: { maximum: 255 }
end
format
app/models/user.rb
class User < ApplicationRecord
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }
end
uniqueness
app/models/user.rb
class User < ApplicationRecord
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: true
end
I need to add an index to email, so execute the following command.
$ rails generate migration add_index_to_users_email
I'm using a Rails method called add_index to add an index to the email column of the users table. unique: You can enforce uniqueness by specifying true.
db/migrate/[timestamp]_add_index_to_users_email.rb
class AddIndexToUsersEmail < ActiveRecord::Migration[6.0]
def change
add_index :users, :email, unique: true
end
end
After modifying the migration file, migrate. When this is done, go to confirmation.
confirmation
users | |
---|---|
id | integer |
name | string |
string | |
created_at | datetime |
updated_at | datetime |
password_digest | string |
A secure password can be almost completed by calling a Rails method called has_secure_password.
You need to add a column called password_digest to the users table. When adding a column, you need to hit the following command.
$ rails generate migration add_password_digest_to_users password_digest:string
db/migrate/[timestamp]_add_password_digest_to_users.rb
class AddPasswordDigestToUsers < ActiveRecord::Migration[6.0]
def change
add_column :users, :password_digest, :string
end
end
Add password_digest column to users table using add_column method. To apply this, run a migration on the database.
After migration, write the password in the user model as follows. This time, I put presence and length in the password.
app/models/user.rb
class User < ApplicationRecord
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: true
validates :password, presence: true, length: { minimum: 6 }
end
That's the end of Chapter 6 of the Rails tutorial, with the tests written accordingly.
To be honest, I feel like I'm developing an app in earnest from Chapter 6, and it's finally the real thing. I want to be patient.
Recommended Posts