0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rails_マイグレーション関連

Posted at

モデル作成(create_table)

command
bundle exec rails g model User

User.rbとマイグレーションファイルが作成される。

create_users.rb
class CreatePosts < ActiveRecord::Migration[6.0]
  create_table :users do |t|
    t.integer :code, null: false, default: 0
    t.string :name, limit: 30, default: ""
    t.string :password, default: ""
    t.text :description, default: ""
    t.date :hire_date
    # t.timestampsで以下2つを追加
    t.datetime :created_at
    t.datetime :updated_at
  end
end

カラム追加、削除、変更

command
bundle exec rails g migration change_columns_to_users(※適当なファイル名)
change_columns_to_users.rb
class ChangeUsersColumns < ActiveRecord::Migration[6.0]
  def change
    add_column :users, :password_digest, :string, after: :name # 追加
    remove_column :users, :password, :string                   # 削除
    change_column :users, :name, limit: 20, default: ""        # 変更
  end
end

マイグレーション実行

command
bundle exec rails db:migrate
            or
bundle exec rake db:migrate

コントローラ作成

command
bundle exec rails g controller Users index

users_controller.rbとusers/index.html.erbが作成される。
その他にもjs、scss、helper、ルーティングなどが作成される。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?