Create insular model classes and seed data

❮ Back Next ❯

In this step, you will create some insular model classes (i.e., ones with no associations) to use in subsequent steps of this activity. This step will have you practice skills introduced in the Model Activity.

Throughout the current activity, we will incrementally implement the following model class design:

In this step, we will create the Airport, Flight, and Passenger classes along with some seed data. We will omit the class associations for now.

Generate the model classes by running these three rails generate model commands in the terminal:

rails generate model Airport name:string airport_code:string city:string
rails generate model Flight departure_time:datetime arrival_time:datetime
rails generate model Passenger first_name:string last_name:string

Reset the database and run the migrations for these model classes by running this command in the terminal:

rails db:migrate:reset

Create sample data with which to seed your database by adding the following code to db/seeds.rb:

# Airports

mem = Airport.create!(
  name: 'Memphis International Airport',
  airport_code: 'MEM',
  city: 'Memphis'
)

atl = Airport.create!(
  name: 'Hartsfield–Jackson Atlanta International Airport',
  airport_code: 'ATL',
  city: 'Atlanta'
)

lax = Airport.create!(
  name: 'Los Angeles International Airport',
  airport_code: 'LAX',
  city: 'Los Angeles'
)

# Flights

mem_atl = Flight.create!(
  departure_time: DateTime.strptime('03-02-2028 09:15:00 AM', '%m-%d-%Y %I:%M:%S %p'),
  arrival_time: DateTime.strptime('03-02-2028 11:05:00 AM', '%m-%d-%Y %I:%M:%S %p')
)

mem_lax = Flight.create(
  departure_time: DateTime.strptime('01-18-2029 06:42:00 AM', '%m-%d-%Y %I:%M:%S %p'),
  arrival_time: DateTime.strptime('01-18-2029 01:16:00 PM', '%m-%d-%Y %I:%M:%S %p')
)

atl_lax = Flight.create(
  departure_time: DateTime.strptime('04-04-2029 10:33:00 AM', '%m-%d-%Y %I:%M:%S %p'),
  arrival_time: DateTime.strptime('04-04-2029 05:00:00 PM', '%m-%d-%Y %I:%M:%S %p')
)

# Passengers

homer = Passenger.create!(first_name: 'Homer', last_name: 'Simpson')
marge = Passenger.create!(first_name: 'Marge', last_name: 'Simpson')
ned = Passenger.create!(first_name: 'Ned', last_name: 'Flanders')
edna = Passenger.create!(first_name: 'Edna', last_name: 'Krabappel')

Run the seeds.rb script by running this command in the terminal:

rails db:seed

Check that the above worked correctly by launching the Rails console:

rails console

Retrieve all of the saved model objects by entering this Ruby code in the console:

Airport.all
Flight.all
Passenger.all

For each of these calls to the all method, you should see output consistent with the seed data above.

Exit the Rails console:

exit

❮ Back Next ❯