A series of flow of table creation → record creation, deletion → table deletion in Ruby on Rails

Introduction

It is a complete muscle training set that also serves as a memorandum to get used to DB operation of Ruby on Rails.

I think the procedure for deleting a table is a little complicated for beginners including myself, so I hope it will be useful for repeated practice.

Since it is assumed that the database is already installed in the Rails application you are using, if you have not created the database yet, please prepare the database with "rails db: create".

environment

Ruby 2.7.2 Rails 6.0.3

Create a new table

First, change from the terminal to the directory containing the Rails app.

console


>cd (folder address)

Create a migration file for model creation. (Example: model name "User", field name "name", "address", "age")

console


> rails g model User name:text address:text age:integer

At this point, a class file and a migration file that define the "User" model are created, but no tables are generated.

Migration is required to generate the table.

console


> rails db:migrate

This will create a "users" table based on the "User" model.

__ * The table name is different from the model name because of the rails naming convention __. Reference: "I tried to summarize the naming convention of Rails DB model" https://qiita.com/seri1234/items/8ca4b52d82390929195f

From record addition to deletion

Launch the Rails console.

Rails console


> rails console

Add a record to the new row.

Rails console


irb(main):001:0> user = User.new
irb(main):002:0> user.name = "John Smith"
irb(main):003:0> user.save

Make sure the record has been added.

Rails console


irb(main):004:0> users = User.all

After confirming, delete the created record. (The argument of find () is the record index)

Rails console


irb(main):005:0> User.find(1).destroy

After deleting the record, exit the Rails console and return to the terminal.

Rails console


irb(main):006:0> exit

From dropping a model to dropping a table

First, delete the file created when the model was created.

console


> rails destroy model User

This will remove the "User" model.

__ Please note that there is still a "users" table at this point. __

Next, create a migration file for deleting the table. ("Drop_table_users" can be any name)

console


> rails generate migration drop_table_users

A migration file called "~ drop_table_users.rb" with numbers at the beginning will be created under "app name / db / migrate /".

Open this file with a text editor, add a command to delete the table in "def change", and save it by overwriting.

1**********9drop_table_users.rb


class DropTableUsers < ActiveRecord::Migration[6.0]
  def change
    #Add one line below
    drop_table :users
  end
end

Perform the migration.

console


> rails db:migrate

The table is deleted, and this completes the flow.

Recommended Posts

A series of flow of table creation → record creation, deletion → table deletion in Ruby on Rails
(Ruby on Rails6) Creating data in a table
Ruby on Rails model creation / deletion command
Recommendation of Service class in Ruby on Rails
[Ruby on Rails] A memorandum of layout templates
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
A record of a brief touch on GCP's Data Flow
Ruby on rails learning record -2020.10.03
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on rails learning record-2020.10.07 ②
Ruby on rails learning record-2020.10.07 ①
Ruby on rails learning record -2020.10.06
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
A note about the seed function of Ruby on Rails
How to display a graph in Ruby on Rails (LazyHighChart)
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Apply CSS to a specific View in Ruby on Rails
Basic knowledge of Ruby on Rails
Ruby on Rails Japanese-English support i18n
[Ruby on Rails] Confirmation page creation
(Ruby on Rails6) Creating a database and displaying it in a view
Erase N + 1 in acts_as_tree of tree structure Gem of Ruby on Rails
[Ruby on Rails] Introduction of initial data
[Rails] Addition of Ruby On Rails comment function
Let's summarize "MVC" of Ruby on Rails
part of the syntax of ruby ​​on rails
Ruby on Rails in Visual Studio Codespaces
[Ruby on Rails] About Active Record callbacks
[Ruby on Rails] Japanese notation of errors
(2021) Ruby on Rails administrator (admin) login creation
Explanation of Ruby on rails for beginners ①
[Ruby on rails] Implementation of like function
Beginners create portfolio in Ruby on Rails
[Ruby on Rails] Logical deletion (withdrawal function)
How to create a query using variables in GraphQL [Using Ruby on Rails]
Implementation of Ruby on Rails login function (Session)
[Beginner Procedure Manual 2] Ruby on Rails: Rails template creation
[Ruby on Rails] Until the introduction of RSpec
Rails new in Ruby on Rails ~ Memorandum until deployment 2
Ruby on Rails ~ Basics of MVC and Router ~
Introducing Rspec, a Ruby on Rails test framework
Rails new in Ruby on Rails ~ Memorandum until deployment 1
[Ruby on Rails] Implement a pie chart that specifies the percentage of colors
How to make a unique combination of data in the rails intermediate table
[Ruby on Rails] How to install Bootstrap in Rails
Create a native extension of Ruby in Rust
[Ruby on Rails] Individual display of error messages
I made a portfolio with Ruby On Rails
Ruby on Rails Incorrect string value error resolution when posting a form in Japanese
Ruby on Rails <2021> Implementation of simple login function (form_with)
Count the number of occurrences of a string in Ruby
[Ruby on Rails] Asynchronous communication of posting function, ajax
[Introduction] Try to create a Ruby on Rails application
One case of solving a migration error in Rails
[Ruby on Rails] How to write enum in Japanese
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Build a Ruby on Rails development environment on AWS Cloud9
Implementation of Ruby on Rails login function (devise edition)