What stores data. For example, like a house. But if you put all the data in your house at once, it's hard to find out what's where.
Storage location for each type of data. At home, it's a room. It is easy to understand and manage if you put it in the relevant place for each type of data. It is called a table.
Name the tables in the database just as there are rooms in the house and each room has a name.
The database interacts with the model to retrieve and store data. It's like a gatekeeper.
Files related to the model are created.
invoke active_record
create db/migrate/20200315054113_create_posts.rb
create app/models/post.rb
invoke test_unit
create test/models/post_test.rb
create test/fixtures/posts.yml
Successful generation with create!
The model file is in app / models / post.rb.
% Rails g model Multiple files related to the model were created when the model name was given.
Db / migrate / 20200315054113_create_posts.rb (migration file) was created in one of them.
Create the contents of the table with the migration file.
The table was likened to a room. In addition, create types and column names to organize and store the data in it.
If the room is cluttered, it will be difficult to take it out and store it.
It is easy to understand because it shows what kind of data will be entered.
integer Numerical amount, number of times string Character (short sentence) User name, email address text Character (long sentence) Post text, description datetime Date and time Creation date and time, update date and time
As the name implies, it is a name. You can decide for yourself.
Now, write the column name and type in the migration file.
class CreatePosts < ActiveRecord::Migration[6.0]
def change
create_table :posts do |t|
t.text :memo
t.timestamps
end
end
end
t. Column type: Enter the column name
I will spend many. Describe the column you want to add.
Now, with the Post table in the database. I will issue a command to create this column in the table.
Reflect the contents written in the migration file.
% Rails db: migrate * Be in the directory of the app you want to create!
Recommended Posts