(Ruby on Rails6) How to create models and tables

Preface

This time, I will leave a memorandum on how to create a database table in Ruby on Rails6.

How to create a model and table

bin / ・ ・ ・ </ strong> is used in the Linux </ strong> development environment. (I don't need this character in the development environment on MAC {Unix}, but I think it's good to have it as knowledge.) Also, the table name should be capitalized, such as "Form".

Creating a model and one type of column

command


$ bin/rails g model Smple_form title:string
$ bin/rails g model table name column name:Data type
or
$ bin/rails generate model Smple_form title:string
$ bin/rails generate model Dable name Column name:Data type

Creating a model and multiple columns

command


$ bin/rails g model Smple_form description:text title:string
$ bin/rails g model table name column name:Data type column name:Data type
or
$ bin/rails g generate Smple_form description:text title:string
$ bin/rails g generate table name column name:Data type column name:Data type

Create table Migration

command


$ rails db:migrate

Sometimes I forget to migrate and get an error. Please be careful.

Creating a migration file

↓ means that multiple columns have been created.

20200926124610_create_Optional s.rb


class CreateGoals < ActiveRecord::Migration[6.0]
  def change
    create_table :goals do |t|
      t.text :description
      t.string :title

      t.timestamps
    end
  end
end

Afterword

Thank you for reading this far. That's all for creating the model and database. It was nice to be able to record multiple column settings. Please make use of it.

My link

Also, there is a link on Twitter / Portfolio, so if you are interested, Please connect. I would be very happy to have friends who can share programming learning.

Twitter Portfolio Github

Recommended Posts