LoginSignup
7
12

More than 3 years have passed since last update.

Railsのseeds.rbの書き方

Posted at

はじめに

Userモデルで書いてみます。

書き方

timesを使って書いてみる。

seeds.rb

if Rails.env == "development"
  10.times do |i|
    User.create!(email: "test#{i + 1}@example.com", password: "password", password_confirmation: "password")
  end
end

作成したユーザー↓


[1] pry(main)> User.all
  User Load (1.2ms)  SELECT "users".* FROM "users"
=> [#<User id: 1, email: "test1@example.com", created_at: "2019-07-11 02:13:55", updated_at: "2019-07-11 02:13:55">,
 #<User id: 2, email: "test2@example.com", created_at: "2019-07-11 02:13:55", updated_at: "2019-07-11 02:13:55">,
 #<User id: 3, email: "test3@example.com", created_at: "2019-07-11 02:13:56", updated_at: "2019-07-11 02:13:56">,
 #<User id: 4, email: "test4@example.com", created_at: "2019-07-11 02:13:56", updated_at: "2019-07-11 02:13:56">,
 #<User id: 5, email: "test5@example.com", created_at: "2019-07-11 02:13:56", updated_at: "2019-07-11 02:13:56">,
 #<User id: 6, email: "test6@example.com", created_at: "2019-07-11 02:13:56", updated_at: "2019-07-11 02:13:56">,
 #<User id: 7, email: "test7@example.com", created_at: "2019-07-11 02:13:56", updated_at: "2019-07-11 02:13:56">,
 #<User id: 8, email: "test8@example.com", created_at: "2019-07-11 02:13:56", updated_at: "2019-07-11 02:13:56">,
 #<User id: 9, email: "test9@example.com", created_at: "2019-07-11 02:13:56", updated_at: "2019-07-11 02:13:56">,
 #<User id: 10, email: "test10@example.com", created_at: "2019-07-11 02:13:56", updated_at: "2019-07-11 02:13:56">]

範囲オブジェクトを使って書いて見る

上と同じ結果になります。

seeds.rb
if Rails.env == "development"
  (1..10).each do |i|
    User.create!(email: "test#{i}@example.com", password: "password", password_confirmation: "password")
  end
end

create と create!

create!を使ったほうがいいと思います。
createだとvalidationに失敗したときに例外が発生しないため、思わぬ不具合が起きました。

今回、emailのバリデーションに一意性を追加しているのですが、createではエラーが出ず、ユーザーも最初の1人しか作成されませんでした。

createでrails db:seedを実行する

seeds.rb

if Rails.env == "development"
  (1..10).each do |i|
    User.create(email: "test@example.com", password: "password", password_confirmation: "password")
  end
end
$ rails db:seed
$ 

作成したユーザー↓

[1] pry(main)> User.all
  User Load (2.9ms)  SELECT "users".* FROM "users"
=> [#<User id: 1, email: "test@example.com", created_at: "2019-07-11 02:33:34", updated_at: "2019-07-11 02:33:34">]

create!でrails db:seedを実行

create!であればちゃんとエラーが出ました。

seeds.rb
if Rails.env == "development"
  (1..10).each do |i|
    User.create!(email: "test@example.com", password: "password", password_confirmation: "password")
  end
end
$ rails db:seed
rails aborted!
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken
/Users/sakakin/rails/sample_sql/db/seeds.rb:3:in `block in <main>'
/Users/sakakin/rails/sample_sql/db/seeds.rb:2:in `<main>'
/Users/sakakin/rails/sample_sql/bin/rails:9:in `<top (required)>'
/Users/sakakin/rails/sample_sql/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

以上です。

7
12
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
7
12