[RUBY] Rails Tutorial (4th Edition) Memo Chapter 6

Rails Tutorial (4th Edition) Personal Notes Describe the part you are interested in and the part you may forget.

You created the Users controller at the end of the previous chapter (5.4). The continuation. From Chapter 6 to Chapter 12, we will create each function of user authentication. Of these, Chapter 6 deals with the user's data model and storage.

Chapter 6 Creating a User Model

Rails already has a mechanism for implementing authentication, but each service requires a lot of custom authentication. This is a reinvention of the wheel, but the Rails tutorial reinvents the wheel because it's easier to implement third-party certification if you know how to do it.

6.1 User model

Rails calls the default data structure that treats it as a data model Model (M in MVC). Rails defaults to ʻActive Record` to interact with this Model. This is something that does CRUD and construction without being aware of SQL.

6.1.1 Database migration

Create a User model to persist the database. Just like when creating the Users controller, $ rails g.

As a reminder The controller is ** plural ** ʻUsers The model is ** singular ** ʻUser

It is customary to say.

$ rails g model User name:string email:string

Pass the attribute used in DB (column name in DB) and the type with : in between.

Although various files are generated, a file named timestamp_create_model name s.rb is created indb / migrate /. Information for creating a DB is defined here.

db/migrate/Time stamp_create_users.rb


class CreateUsers < ActiveRecord::Migration[5.0]
  def change
    create_table :users do |t|
      t.string :name
      t.string :email

      t.timestamps
    end
  end
end

The id and time stamp are automatically prepared even if you do not specify them. Looking at the class name part, it seems that it inherits ʻActiveRecord :: Migration [5.0]. (5.0 in Rails tutorial, but 5.1 notation for my environment) timestamps creates two columns for timestamps [ created_at, ʻupdated_at].

Is it like this with mysql?

mysql


CREATE DATABASE User;
USE Users;
CREATE TABLE users(
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(300),
  email VARCHAR(300),
  created_at TIMESTAMP NOT NULL CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
);

Every time I write sql, I'm writing that an uncle who doesn't allow uppercase or lowercase letters is likely to occur (I was also in my previous job). To be honest, I think it's okay to use all lowercase letters in this age. ~~ By the way, in my previous job, write the date in varchar () and stop doing crap ~~

Apply migration. According to a friend, I was reminded that it was a command to explode with special care in multi-person development. Let's be careful. The DB side is not version controlled.

$ rails db:migrate

6.1.2 model file

** Exercise ** I did ʻUser.new` in the Rails console and checked what kind of inheritance relationship it has.

ʻUser class <ʻApplicationRecord <ʻActiveRecord :: Base <ʻObject <BaseObject

6.1.3 Create a user object

rails console sandbox options

Will roll back all changes to the DB at the end.

$ rails console --sandbox

Experiment in this. The user class was created when the model was created (ʻapp / model / ), which is inherited from ʻActiveRecord.

Method used below valid?: Confirmation of validity (Validity) save: Save When saved, a time stamp is stamped on create_at, ʻupdate_at`. In the return value, the bool value of whether or not it was saved is returned.

create: User.new only creates it in memory, but saves it as soon as it is created.

** Exercise ** User.create user.name ActiceSupport::timeWithZone

6.2 Validate users

6.2.1 Validate

In short, it's like specifying a column type in SQL. For : email, Double or Boolean is not included. The following 4 points

--Presence --Length --Format --Uniqueness

Impressions

--I feel like I need a delete flag in the users table. Will you add it later? ――It's easy to do the migrate file of db, id to write all the time, and you don't have to specify the number of characters with varchar. --I am very relieved that the migrate file of db and the changes will remain in chronological order. In my previous job, I didn't even leave the SQL when I changed the DB.

Recommended Posts

Rails Tutorial (4th Edition) Memo Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
Rails Tutorial 6th Edition Learning Summary Chapter 4
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails Tutorial 6th Edition Learning Summary Chapter 3
Rails Tutorial 6th Edition Learning Summary Chapter 8
Rails Tutorial (4th Edition) Summary
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 4th Edition: Chapter 1 From Zero to Deployment
rails tutorial Chapter 8
Rails tutorial (6th edition) Follow/unfollow background operation
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)
[Rails Tutorial Chapter 4] Rails-flavored Ruby
Rails tutorial (6th edition) Background operation of profile editing
Rails tutorial (6th edition) Background operation of login function
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
rails tutorial
[Rails Tutorial Chapter 5] Create a layout
rails tutorial chapter 10 summary (for self-learning)
rails tutorial
rails tutorial
Chewing Rails Tutorial [Chapter 2 Toy Application]
rails tutorial
rails tutorial
rails tutorial
Rails tutorial (6th edition) Background operation of password reset function
Rails Tutorial (6th Edition) -Explanation of background operation for each function-
Rails Tutorial Chapter 0: Preliminary Basic Knowledge Learning 5
docker tutorial (memo)
Rails tutorial test
Rails tutorial memorandum 1
Rails tutorial memorandum 2
Rails tutorial (6th edition) Background operation of the posting function of the micro post
Start Rails Tutorial
[Beginner] Rails Tutorial
[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 Tutorial Chapter 1 From Zero to Deployment [Try]
Chewing Rails Tutorial [Chapter 3 Creating Almost Static Pages]
[Rails tutorial] A memorandum of "Chapter 11 Account Activation"
[Learning Memo] Metaprogramming Ruby 2nd Edition: Chapter 3: Methods
Resolve Gem :: FilePermissionError when running gem install rails (Rails Tutorial Chapter 1)
[Ruby on Rails Tutorial] Error in the test in Chapter 3