はじめに
おはようございます!torihaziです。
現在、やっているecサイト課題で seed というものを使ったので
その備忘録です!
読了時間は10しないと思います!
理解は秒です!
ではどうぞ! ltg!
そもそもseedとは
データベースを作成したときに、テストデータを何個か入れたい!
てなった時にいくつかドバーッとテストデータを流し込んでくれるものです
使い方
そもそもどこにあるのかというとdb/seeds.rbです。
そこにモデル.createの要領で書けば良いです。
その後に rails db:seedを実行すれば勝手にデータベースに追加してくれます。
ただ1、2個程度追加するためだけに seeds.rbを使用するのはめんどくさいです。
これらが真価を発揮する時は数十個を追加するときです。
例えばこんなとき
User.create!(firstname: "佐藤", lastname: "たける", age: 14) User.create!(firstname: "後藤", lastname: "たけし", age: 44) User.create!(firstname: "内藤", lastname: "りょうた", age: 34) User.create!(firstname: "加藤", lastname: "たけ", age: 23) ・・・ ・・・ ・・・ ・・・ あと50件くらい
これを1行1行書く気が起きますか?
起きないですよね。
ただ、もちろん上の書き方でもできます。
それがだるいのでこんな感じで描きます。
Fakerというgemを入れてください!
50.times do
User.create(
firstname: Faker::Name.first_name,
lastname: Faker::Name.last_name,
email: Faker::Internet.email(domain: "gmail.com")
)
end
ただこのままだと日本語でないのでconfig/application.rbに下記を追加します。
module Myapp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.0
config.i18n.default_locale = :ja <= これ
end
end
そうすると、一気に次のようにデータをぶち込めます。

すぐに試したい方へ
環境は私の過去記事を元にやってみてください。
cd ~/Document git clone https://github.com/torihazi/rails-docker.git tmp docker compose up -d --build docker compose run web rails db:create docker compose run web rails generate scaffold User firstname:string lastname:string email:string docker compose run web rails db:migrate
その後に次のファイルを編集してください
app/views/users/index.html.erb
<p style="color: green"><%= notice %></p>
<h1>Users</h1>
<div id="users">
<table class="design01">
<tr>
<th>firstname</th>
<th>lastname</th>
<th>email</th>
</tr>
<% @users.each do |user| %>
<tr>
<td><%= user.firstname %></td>
<td><%= user.lastname %></td>
<td><%= user.email %></td>
</tr>
<% end %>
</table>
</div>
<%= link_to "New user", new_user_path %>
app/assets/stylesheets/application.css
.design01 {
width: 100%;
text-align: center;
border-collapse: collapse;
border-spacing: 0;
}
.design01 th {
padding: 10px;
background: #e9faf9;
border: solid 1px #778ca3;
}
.design01 td {
padding: 10px;
border: solid 1px #778ca3;
}
Gemfile 追加して bundle install
ファイル末尾に gem `faker'
config/application.rb
module Myapp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.0
config.i18n.default_locale = :ja <= これ追加
# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
# in config/environments, which are processed later.
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
end
end
db/seeds/rb
50.times do
User.create(
firstname: Faker::Name.first_name,
lastname: Faker::Name.last_name,
email: Faker::Internet.email(domain: "gmail.com")
)
end
ここまで終わったら最後に
docker compose run web rails db:seed
を実行して結果を確認してください
このようにすれば行けます。行けなかったら連絡ください( ´ ▽ ` )

終わりに
どうでしたか?
やってみれば行けます。
私は初心者です!ゆえに誰でも理解できます!!
応援してます!!!
私も頑張ります!!