[RUBY] [Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 6

Introduction

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.

User model creation

The basics of the user model data structure.

users
id integer
name string
email 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.

Add validation to User model

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
email 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.

Summary

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

[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 6
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 3
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapters 4 and 5
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
rails tutorial Chapter 6
rails tutorial Chapter 1
rails tutorial Chapter 7
rails tutorial Chapter 5
rails tutorial Chapter 10
rails tutorial Chapter 9
rails tutorial Chapter 8
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
Rails Tutorial Chapter 5 Notes
Rails Tutorial Chapter 10 Notes
Rails Tutorial Chapter 3 Notes
Rails Tutorial Chapter 3 Learning
Rails Tutorial Memorandum (Chapter 3, 3.1)
Rails Tutorial Chapter 4 Notes
Rails Tutorial Chapter 4 Learning
Rails Tutorial Chapter 1 Learning
Rails Tutorial Chapter 2 Learning
Rails Tutorial Chapter 8 Notes
Rails Tutorial Memorandum (Chapter 3, 3.3.2)
[Ruby on Rails Tutorial] Error in the test in Chapter 3
What you learned about hashes
What you learned about symbols
[Rails Tutorial Chapter 4] Rails-flavored Ruby
What I learned from studying Rails
[Rails Tutorial Chapter 5] Create a layout
rails tutorial chapter 10 summary (for self-learning)
Chewing Rails Tutorial [Chapter 2 Toy Application]
Rails Tutorial (4th Edition) Memo Chapter 6
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
What to do if Cloud9 is full in the Rails tutorial
[Note] What to do if bundle install in Chapter 3 of the rails tutorial is not possible
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails Tutorial 6th Edition Learning Summary Chapter 4
rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
What is follow_redirect! Following ?: Rails Tutorial Memorandum-Chapter 7
[Rails Struggle/Rails Tutorial] Summary of Heroku commands
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails refactoring story learned in the field
rails tutorial
What Rails beginners learned by solving errors
rails tutorial
How to use MySQL in Rails tutorial
rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 2
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 3
rails tutorial
[Cloud9] Yay! You ’re on Rails! Is not displayed in the rails tutorial
rails tutorial
Rails Tutorial 6th Edition Learning Summary Chapter 8
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 11]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 1]
(Giri) A local government employee in his twenties works on a Rails tutorial [Chapter 14]