Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4 create seed file #23

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
# This file should ensure the existence of records required to run the application in every environment (production,
# development, test). The code here should be idempotent so that it can be executed at any point in every environment.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Example:
#
# ["Action", "Comedy", "Drama", "Horror"].each do |genre_name|
# MovieGenre.find_or_create_by!(name: genre_name)
# end
ADDRESSES = [
{line_1:"1221 E Elizabeth St", city: "Fort Collins", state: "CO", zip_code: "80524"},
{line_1:"2121 E Harmony Rd # 180", city: "Fort Collins", state: "CO", zip_code: "80528"},
{line_1:"2315 E Harmony Rd Suite 110", city: "Fort Collins", state: "CO", zip_code: "80528"},
{line_1:"608 E Harmony Rd #101", city: "Fort Collins", state: "CO", zip_code: "80525"},
{line_1:"1106 E Prospect Rd", city: "Fort Collins", state: "CO", zip_code: "80525"},
{line_1:"1939 Wilmington Dr", city: "Fort Collins", state: "CO", zip_code: "80528"},
{line_1:"1107 S Lemay Ave", line_2: "Suite 240", city: "Fort Collins", state: "CO", zip_code: "80524"},
{line_1:"1024 S Lemay Ave", city: "Fort Collins", state: "CO", zip_code: "80524"},
{line_1:"4601 Corbett Dr", city: "Fort Collins", state: "CO", zip_code: "80528"},

].freeze

# Create Drivers
ActiveRecord::Base.connection.transaction do
puts "Creating Drivers..."
3.times do
first_name = Faker::Name.first_name
last_name = Faker::Name.last_name
Driver.create!(first_name:,last_name:)
end

# Create home address for driver
puts "Creating current Addresses for Drivers..."
Driver.find_each.with_index do |driver, index|
address = Address.create(ADDRESSES[index])
driver.create_current_driver_address(address:, current: true)
end

puts "Creating remaining Addresses..."
ADDRESSES[(Address.count + 1)..].each do |attrs|
Address.create(**attrs)
end

puts "Creating Rides..."
Address.find_each do |from_address|
to_address = Address.where.not(id: from_address.id).order("RANDOM()").limit(1).first
Ride.create!(from_address:, to_address:)
end
end
Loading