1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Rails】seeds.rbについて

Posted at

記事概要

Ruby on Railsのシード値について、まとめる

前提

  • Ruby on Railsでアプリケーションを作成している

基本情報

ファイルパス

db/seeds.rb

記載方法

事前作業

ファイルに記述されている内容は全て削除

データ生成コードの記述

createメソッドを使用し、データ生成のコードを記述する

seed.rb
Item.create(name: 'ペン', price: 100)
Item.create(name: 'ペン2', price: 200)
Item.create(name: 'ノート1', price: 400)
Item.create(name: 'ノート2', price: 600)

コードの適用

  1. DB作成やマイグレーションを行っていない場合は、下記を実施する
    # DB生成
    % rails db:create
    
    # マイグレーション
    % rails db:migrate
    
  2. シード値をDBに登録する
    % rails db:seed
    

Ruby on Railsまとめ

DB

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?