LoginSignup
1
2

More than 3 years have passed since last update.

[学習用]コピペで使いたいRails5 seeds.rb データ集

Last updated at Posted at 2018-07-03

テストデータ登録するのに使うseed
まだだいたい同じなので忘れない様に作ったものコピペ用メモ
使ったら追加しておきたい。

ランダムで値を入れる(未確認)

seeds.rb
10.times do
  Staff.create(
    name: Faker::Name.name,
    age: Faker::Number.between(18, 60),
    joined_on: Faker::Date.between(20.days.ago, Date.today)
  )
end

between(18, 60)で18歳~60歳をランダムで入れられる。
Date.between(20.days.ago, Date.today)で日付を今日から20日前までランダムに入れられる。
まだこの方法は試してないので未確認
FakerというGemが必要で、それっぽいユーザーデータが作る。

参考:https://qiita.com/schiughi/items/86ca88b3ccd0a491ea4f
参考:http://maru877.hatenablog.com/entry/2017/07/02/152020

会社名・住所・電話番号を10個入れる

seeds.rb
10.times do |i|
  Company.create(
      companyname: "会社名 #{i}",
      zip: "123-456#{i}",
      sentence: "一言文章 #{i}",
      prefectures: "東京都",
      address: "東京都中央区#{i}",
      category: "1",
      # category: "#{i}",
      tell: "03-1234-456#{i}",
      fax: "03-4567-789#{i}",
      mailaddress: "info@test#{i}.com",
      website: "https://test#{i}.com"
  )
end

Company.createのCompanyを自分のカラム名に変える。

会社名・住所・電話番号を1個入れる

seeds.rb
Company.create(companyname: '会社名',  sentence: '一言文章',zip: '1234567',prefectures: '東京都',address: '東京都中央区中央1-2-3',category: '1',tell: '0120123456',fax: '0120654321',mailaddress: 'info@test.com',website: 'https://test.com/')

Company.createのCompanyを自分のカラム名に変える。

メール・パスワード・名前・フラグ

seeds.rb
User.create(email: 'admin@test.com', password: 'password', name: '管理者', admin: true)
User.create(email: 'writer@test.com', password: 'password', name: 'ライター', admin: true)
User.create(email: 'worker@test.com', password: 'password', name: '編集者', admin: true)
User.create(email: 'test@test.com', password: 'password', name: '桜田佐藤', admin: false)
User.create(email: 'test2@test.com', password: 'password', name: '横井鈴木', admin: false)

アドミンフラグを使ってグループ化してるとき

メール・パスワード・名前

seeds.rb
#Admin
Admin.create(email: 'admin@test.com', password: 'password', name: '管理者')
#User
User.create(email: 'satou@test.com', password: 'password', name: '佐藤武田')
User.create(email: 'suzuki@test.com', password: 'password', name: '鈴木田中')
User.create(email: 'tanaka@test.com', password: 'password', name: '田中斎藤')
User.create(email: 'test@test.com', password: 'password', name: 'テスト')

メール・パスワード

seeds.rb
User.create(email: 'admin@test.com', password: 'password')
User.create(email: 'satou@test.com', password: 'password')
User.create(email: 'suzuki@test.com', password: 'password')
User.create(email: 'tanaka@test.com', password: 'password')
User.create(email: 'test@test.com', password: 'password')

名前・タイトル・コンテンツ

seeds.rb
10.times do |i|
  Post.create(
          name: "名前 #{i}",
          title: "タイトル #{i}",
          content: "コンテンツ #{i}"
  )
end

わからないこと

ポスト入れつつ、コメントのサンプルも入れたいのに
どうすれば入るのかわからない。

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