Skip to content

Latest commit

 

History

History
203 lines (176 loc) · 6.46 KB

06-04-seed.md

File metadata and controls

203 lines (176 loc) · 6.46 KB

Unit 6

Chapter 4: Model Seeding

In this chapter, you will seed the database in your development environment with simulated user and admin model data. As you will see later, this fake data helps you to see how your site will appear under active use.

New Branch

Enter the command "git checkout -b 06-04-seed".

Gemfile

  • Add the Faker gem. Add the following lines to the end of the Gemfile:
# BEGIN: gems used in db/seeds.rb
group :test, :development do
  gem 'faker' # Generates fake data used for seeding the database
  gem 'ruby-progressbar' # Provides a progress bar to be used during long loop actions
end
# END: gems used in db/seeds.rb
  • Enter the command "sh git_check.sh".
  • Enter the following commands:
git add .
git commit -m "Added the Faker and ruby-progressbar gems"

db/seeds.rb

Replace the contents of the db/seeds.rb file with the following:

require 'ruby-progressbar'

########################
# BEGIN: creating admins
########################

puts '----------------------------------'
puts 'Creating super admin (Jill Tarter)'
Admin.create!(last_name: 'Tarter', first_name: 'Jill',
              username: 'jill_tarter',
              email: '[email protected]',
              password: 'SETI Institute',
              password_confirmation: 'SETI Institute',
              confirmed_at: Time.now, super: true)

puts '----------------------------------'
puts 'Creating super admin (Frank Drake)'
Admin.create!(last_name: 'Drake', first_name: 'Frank',
              username: 'frank_drake',
              email: '[email protected]',
              password: 'Drake Equation',
              password_confirmation: 'Drake Equation',
              confirmed_at: Time.now, super: true)

n_admins = 50
puts '--------------------------------------------'
puts "Creating the first #{n_admins} random admins"
pbar = ProgressBar.create(total: n_admins)
n_admins.times do |n|
  name_l = Faker::Name.last_name
  name_f = Faker::Name.first_name
  email_address = "admin-#{n + 1}@rubyonracetracks.com"

  Admin.create!(last_name: name_l, first_name: name_f,
                username: "admin#{n + 1}",
                email: email_address, password: 'Daytona 500',
                password_confirmation: 'Daytona 500',
                confirmed_at: Time.now)
  pbar.increment
end

n_admins = 51
puts '------------------------------------'
puts "Creating the second #{n_admins} random admins"
pbar = ProgressBar.create(total: n_admins)
n_admins.times do |n|
  name_l = Faker::Name.last_name
  name_f = Faker::Name.first_name
  email_address = Faker::Internet.email(name_f)

  Admin.create!(last_name: name_l, first_name: name_f,
                username: "admin-faker#{n + 1}",
                email: email_address, password: 'Daytona 500',
                password_confirmation: 'Daytona 500',
                confirmed_at: Time.now)
  pbar.increment
end

###########################
# FINISHED: creating admins
###########################

#######################
# BEGIN: creating users
#######################

puts '-----------------------------'
puts 'Creating user (Ellie Arroway)'
User.create!(last_name: 'Arroway', first_name: 'Ellie',
             username: 'earroway',
             email: '[email protected]',
             password: '3.14159265',
             password_confirmation: '3.14159265',
             confirmed_at: Time.now)

puts '----------------------------'
puts 'Creating user (Example User)'
User.create!(last_name: 'User', first_name: 'Example',
             username: 'example_user',
             email: '[email protected]',
             password: 'Daytona 500',
             password_confirmation: 'Daytona 500',
             confirmed_at: Time.now)

n_users = 52
puts '--------------------------------------'
puts "Creating first #{n_users} random users"
pbar = ProgressBar.create(total: n_users)
n_users.times do |n|
  name_l = Faker::Name.last_name
  name_f = Faker::Name.first_name
  email_address = "example-#{n + 1}@railstutorial.org"

  User.create!(last_name: name_l, first_name: name_f,
               username: "user#{n + 1}", email: email_address,
               password: 'Daytona 500',
               password_confirmation: 'Daytona 500',
               confirmed_at: Time.now)
  pbar.increment
end

n_users = 53
puts '---------------------------------------'
pbar = ProgressBar.create(total: n_users)
puts "Creating second #{n_users} random users"
n_users.times do |n|
  name_l = Faker::Name.last_name
  name_f = Faker::Name.first_name
  email_address = Faker::Internet.email(name_f)

  User.create!(last_name: name_l, first_name: name_f,
               username: "user-faker#{n + 1}", email: email_address,
               password: 'Daytona 500',
               password_confirmation: 'Daytona 500',
               confirmed_at: Time.now)
  pbar.increment
end

##########################
# FINISHED: creating users
##########################

seed.sh

  • Create the file seed.sh with the following content:
#!/bin/bash

sh pg-start.sh

echo '----------------------'
echo 'rails db:migrate:reset'
rails db:migrate:reset

echo '-------------'
echo 'rails db:seed'
rails db:seed
  • Enter the command "sh seed.sh" to run this script.

Rails Console

  • If you don't already have a Rails console open, enter the command "sh sandbox.sh".
  • In the Rails sandbox console, enter the command "User.all". You'll see a list of all users.
  • In the Rails sandbox console, enter the command "Admin.all". You'll see a list of all admins.
  • Return to the normal command line to continue on.

all.sh

  • Add the seed.sh script to all.sh. Add the following code after the build_fast.sh section and before the test_code.sh section:
FILE_LOG_SEED='log/all-seed.log'
echo '---------------------------'
echo "sh seed.sh > $FILE_LOG_SEED"
sh seed.sh > $FILE_LOG_SEED
  • Enter the command "sh all.sh".
  • Enter the command "sh git_check.sh".
  • Enter the following commands:
git add .
git commit -m "Added seeding capability and scripts"

Wrapping Up

  • Enter the command "git push origin 06-04-seed".
  • Go to the GitHub repository and click on the "Compare and pull request" button for this branch.
  • Accept this pull request to merge it with the master branch, but do NOT delete this branch.
  • Enter the following commands:
git checkout master
git pull
sh heroku.sh

Conclusions

  • In all subsequent chapters, it will be assumed that you have used the seed.sh script to seed the database.
  • The database seeding task is included in the all.sh command.