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

More than 1 year has passed since last update.

Rails サンプルデータの投入方法

Posted at

あらかじめテーブルは作成しておく
今回はUserテーブル(カラムはnameとemail)にデータを10個ずつ投入する

db/seeds.rb
10.times do |n|
  name  = "testuser#{n+1}"
  email = "testuser#{n+1}@example.com"
  User.create!(name:  name,
               email: email
              )
end

以下のコマンドを行う
rails db:migrate:reset
rails db:seed

実行結果

$ rails db:migrate:reset
Dropped database 'agribeit_development'
Dropped database 'agribeit_test'
Created database 'agribeit_development'
Created database 'agribeit_test'
== 20230325044645 CreateUsers: migrating ======================================
-- create_table(:users)
   -> 0.0113s
== 20230325044645 CreateUsers: migrated (0.0114s) =============================

$ rails db:seed
$

データの中身

mysql> select * from users;
+----+------------+------------------------+----------------------------+----------------------------+
| id | name       | email                  | created_at                 | updated_at                 |
+----+------------+------------------------+----------------------------+----------------------------+
|  1 | testuser1  | testuser1@example.com  | 2023-04-08 12:06:24.941848 | 2023-04-08 12:06:24.941848 |
|  2 | testuser2  | testuser2@example.com  | 2023-04-08 12:06:24.948444 | 2023-04-08 12:06:24.948444 |
|  3 | testuser3  | testuser3@example.com  | 2023-04-08 12:06:24.954578 | 2023-04-08 12:06:24.954578 |
|  4 | testuser4  | testuser4@example.com  | 2023-04-08 12:06:24.959667 | 2023-04-08 12:06:24.959667 |
|  5 | testuser5  | testuser5@example.com  | 2023-04-08 12:06:24.965160 | 2023-04-08 12:06:24.965160 |
|  6 | testuser6  | testuser6@example.com  | 2023-04-08 12:06:24.969590 | 2023-04-08 12:06:24.969590 |
|  7 | testuser7  | testuser7@example.com  | 2023-04-08 12:06:24.974432 | 2023-04-08 12:06:24.974432 |
|  8 | testuser8  | testuser8@example.com  | 2023-04-08 12:06:24.979816 | 2023-04-08 12:06:24.979816 |
|  9 | testuser9  | testuser9@example.com  | 2023-04-08 12:06:24.984884 | 2023-04-08 12:06:24.984884 |
| 10 | testuser10 | testuser10@example.com | 2023-04-08 12:06:24.989177 | 2023-04-08 12:06:24.989177 |
+----+------------+------------------------+----------------------------+----------------------------+
10 rows in set (0.00 sec)
0
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
0
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?