Create new association links between model objects

❮ Back Next ❯

In this step, you will learn some different ways to create association links between model objects.

The Rails console should still be running from the previous step.

In this part, we will create a new flight and set its departure airport as LAX.

Create a new Flight object lax_mem without yet saving it to the database by entering this Ruby code into the console:

lax_mem = Flight.new(
  departure_time: DateTime.strptime('08-22-2029 6:45:00 AM', '%m-%d-%Y %I:%M:%S %p'),
  arrival_time: DateTime.strptime('08-22-2028 2:02:00 PM', '%m-%d-%Y %I:%M:%S %p')
)

Pretty print the lax_mem object:

lax_mem

Note that the attributes id, created_at, and updated_at are all currently set to nil because we have not yet saved the object to the database. Also, departure_airport_id is nil because we have not yet set the departure airport.

Retrieve the LAX Airport object by entering this Ruby code into the console:

lax = Airport.find_by(airport_code: 'LAX')

Create an association link between the Flight object lax_mem and its departure Airport object lax by entering this Ruby code into the console:

lax_mem.departure_airport = lax

Pretty print the lax_mem object:

lax_mem

Note that the Flight object lax_mem now has the value 3 set for its departure_airport_id, which is the id of the LAX Airport object. Also, note that the id and timestamp attributes remain nil because we still have not saved the object to the database.

Save the Flight object lax_mem to the database by entering this Ruby code into the console:

lax_mem.save

Note that the output indicates that a SQL INSERT command was run.

Pretty print the lax_mem object:

lax_mem

Note that the lax_mem object’s id and timestamp attributes have now been assigned values, indicating that the object was saved to the database.

Add a new object to a has_many collection using the << operator

Let’s save another departure flight to the LAX Airport model object, this time using the << operator.

Create a new Flight object lax_atl without yet saving it to the database by entering this Ruby code into the console:

lax_atl = Flight.new(
  departure_time: DateTime.strptime('07-17-2028 10:10:00 AM', '%m-%d-%Y %I:%M:%S %p'),
  arrival_time: DateTime.strptime('07-17-2028 2:32:00 PM', '%m-%d-%Y %I:%M:%S %p')
)

Add this Flight object to the lax object’s departure_flights collection by invoking the << operator:

lax.departure_flights << lax_atl

Note that invoking the << operator resulted in a SQL INSERT command being executed—the lax_atl object was automatically saved to the database.

Pretty print the Flight object lax_atl:

lax_atl

Note that the object has an id value of 5 (indicating that it was saved to the database), and its departure_airport_id is set to 3 (the id of the Airport object lax).

Add a new object to a has_many collection using the create method

Yet another way we can add new departure Flight objects to our Airport objects is using the create method. To demonstrate, we will add another new Flight to the Airport object lax.

Create a new Flight object and add it to the lax object’s departure_flights by calling the create method:

lax.departure_flights.create(
  departure_time: DateTime.strptime('03-28-2028 9:44:00 AM', '%m-%d-%Y %I:%M:%S %p'),
  arrival_time: DateTime.strptime('03-28-2028 4:06:00 PM', '%m-%d-%Y %I:%M:%S %p')
)

Note that the call to create resulted in a SQL INSERT command being run, indicating that a new Flight object was saved to the database. Also, note that the console output includes the newly created object (because create returned it) and that object has an id of 6 (further confirming that it was saved) and a departure_airport_id of 3 (the id of the lax airport).

Keep the console running, as we will continue to use it in the next step.


❮ Back Next ❯