You can create any number of them in the database in a tabular storage location. Even if the database exists, you cannot save the data without this table.
The table is in tabular format, and the one row next to it is called a record. Also. A vertical column is called a column. A column called id is provided to identify the records in the table.
DOA DOA is a data-centric approach to designing data before programming. It is expected that service development will be more efficient.
As a procedure
Extract table If you add a table in the middle, you will have to review the relationship. Don't use reserved words.
Table definition Determine the columns that each table has. If you add it in the middle, you may have to rewrite the code or change the view. Don't use reserved words.
Organize the table structure Do not create the same column name in the same table. In such a case, let's make another table. EX) Image column, image2 column, etc. in the same table. In this case, create an image table.
Create ER diagram A diagram that clearly shows the relationship between the tables. Write in IE notation.
Model naming convention Model class name starts with uppercase singular Model class filenames are singular with lowercase letters Table name is plural with lowercase letters EX) Animal model class name animal model class filename animals table name
There are commands to create these model files and tables.
That is the rails g model
command. Follow this command with the name of the model class you want to create, all in lowercase.
EX)rails g model animal
I said that hitting this command will create a table, but the table itself hasn't been created yet, just the blueprint for the table. The blueprint of the table is called a migration file.
You can specify the columns to create using the change method.
Column type integer number id etc. string Less characters User name, password text A lot of characters posted boolean True or false or true / false flag datetime Date and time Creation date and time, update date and time
2020XXXXXXXXXXXXXXXX_create_XXXX.rb
class CreateXxxxxx < ActiveRecord::Migration[5.2]
def change
create_table :xxxxs do |t|
t.string :name
t.text :text
t.text :image
t.timestamps null: false
end
end
end
rake db:migrate
This command updates the file.
The schema file records the latest migration file version.
schema_migrations
is like a database change history, recording which migration files are being executed.
Migration files should not be deleted as they may conflict with schema_migrations.
Once the migration file is executed with rake db: migrate
, the contents cannot be rewritten and re-executed.
You can use rake db: rollback
to restore the database state before running the latest migration file.
Active Record Active Record is a type of Ruby Gem. This gem is installed by default in Rails, and thanks to this gem, models and tables are spliced together. This allows Rails to access the records in the table. To use Actice Record, you need to inherit a class called ApplicationRecord. By inheriting ApplicationRecord, all method, new method, save method, find method, etc. can be used.
SQL The language used to request data stored in a database from the database. Rails makes it easy to request data thanks to Active Record.
Recommended Posts