Kaynağa Gözat

Feed the database with seed, and fix a foreign_key issue

Lou 3 yıl önce
ebeveyn
işleme
27c86b0a5b
3 değiştirilmiş dosya ile 21 ekleme ve 9 silme
  1. 1
    1
      db/migrate/20210413142821_create_activities.rb
  2. 0
    1
      db/schema.rb
  3. 20
    7
      db/seeds.rb

+ 1
- 1
db/migrate/20210413142821_create_activities.rb Dosyayı Görüntüle

@@ -4,7 +4,7 @@ class CreateActivities < ActiveRecord::Migration[6.1]
4 4
       t.string :name, index: true, null: false
5 5
       t.string :client
6 6
       t.string :description
7
-      t.references :author, null: false, foreign_key: { to_table: :users }
7
+      t.references :author, null: false
8 8
 
9 9
       t.timestamps
10 10
     end

+ 0
- 1
db/schema.rb Dosyayı Görüntüle

@@ -33,5 +33,4 @@ ActiveRecord::Schema.define(version: 2021_04_13_142821) do
33 33
     t.index ["username"], name: "index_users_on_username", unique: true
34 34
   end
35 35
 
36
-  add_foreign_key "activities", "users", column: "author_id"
37 36
 end

+ 20
- 7
db/seeds.rb Dosyayı Görüntüle

@@ -1,7 +1,20 @@
1
-# This file should contain all the record creation needed to seed the database with its default values.
2
-# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
3
-#
4
-# Examples:
5
-#
6
-#   movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
7
-#   Character.create(name: 'Luke', movie: movies.first)
1
+User.delete_all
2
+User.reset_pk_sequence
3
+Activity.delete_all
4
+Activity.reset_pk_sequence
5
+
6
+10.times do |i|
7
+  name = Faker::Name.first_name.downcase
8
+  user = User.create! username: "#{name}", email: "#{name}@email.com", password: "azerty"
9
+  puts "Created user ##{i} - #{user.username}"
10
+  
11
+  2.times do
12
+    activity = Activity.create!(
13
+      name: Faker::Science.element,
14
+      client: Faker::Company.name,
15
+      description: Faker::Company.catch_phrase,
16
+      author_id: user.id
17
+    )
18
+    puts "Created activity \"#{activity.name}\" for #{activity.client}"
19
+  end
20
+end

Loading…
İptal
Kaydet