[RUBY] Rails database basics

table

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.

Records and columns

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.

Database design

As a procedure

  1. Extract table If you add a table in the middle, you will have to review the relationship. Don't use reserved words.

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

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

  4. Create ER diagram A diagram that clearly shows the relationship between the tables. Write in IE notation.

model

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.

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

Migration file settings

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

Executing the migration file

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

Rails database basics
Rails basics
Rails CSV basics
Rails Routing Basics
Rails Logger Basics
Rspec Basics [Rails]
Ruby on Rails basics
[Rails] Introduction of devise Basics
Add & save from rails database creation
[Rails / MySQL] Database logical name setting
[Rails g.error]
Rails Review 1
Rails API
Rails migration
Ruby basics
Ruby basics
[Rails] first_or_initialize
rails tutorial
Fragment basics
JPA Basics 1
About Rails 6
Docker basics
ViewPager basics
Rails foundation
Rails memorandum
rails tutorial
Java basics
rails tutorial
rails tutorial
Java basics
RSpec Basics
Rails Basics of creating a new application
JavaScript basics
[Rails] devise
rails tutorial
rails test fails with database reference error
JPA Basics 2
rails tutorial
Hash basics
Java basics
[Rails] Reset the database in the production environment
Rails Tips
rails method
rails tutorial
Ruby basics
[Rails] ActiveRecord
[Rails] form_with
RecyclerView Basics
Rails Review 2
[Rails] rails new to create a database with PostgreSQL
[Rails] I learned about database data type types!
Ruby on Rails ~ Basics of MVC and Router ~
[Rails] Added in devise: username not added to database