Feed the database with seed, and fix a foreign_key issue

This commit is contained in:
Lou 2021-04-27 17:08:23 +02:00
commit 27c86b0a5b
3 changed files with 21 additions and 9 deletions

View file

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

View file

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

View file

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