Retrieve model objects using association links

❮ Back Next ❯

In this step, given a model object with one or more association links to other objects, you will learn how to retrieve the other objects using the Rails model API.

Recall that our Flight and Airport objects are currently configured as follows (as per the seeds.rb script):

We will use the association link(s) connecting Flight and Airport objects to demonstrate how, given one linked object, we can retrieve the object(s) on the other end of the link(s).

Launch the Rails console:

rails console

Given a Flight object, retrieve its associated Airport object

Retrieve a Flight object by entering this Ruby code into the console. We happen to choose the first Flight object in the database (but this choice is not important here).

my_flight = Flight.first

Pretty print the retrieved Flight object:

my_flight

Note that the object retrieved is the mem_atl object from the seeds.rb script, also depicted in object diagram above. Also note that this Flight object has an association link with the Airport object atl.

Retrieve this Flight object’s associated departure Airport object by entering into the console this call to the departure_airport method:

my_airport = my_flight.departure_airport

Note that we declared the departure_airport method in the Flight class belongs_to declaration (see the previous step).

Pretty print the retrieved Airport object:

my_airport

Note that, as expected, the Airport object mem was retrieved by the departure_airport method call.

Given an Airport object, retrieve its associated Flight objects

We will use the my_airport object, which references the mem object in the above object diagram. As per the diagram, mem has association links with the Flight objects mem_atl and mem_lax, so these will be the two objects we expect to retrieve.

Retrieve the flights that depart from the airport my_airport by entering into the console this call to the departure_flights method:

my_flight_array = my_airport.departure_flights

Note that we declared the departure_flights method in the Airport class has_many declaration (see the previous step).

Pretty print the retrieved Flight array:

my_flight_array

Note that an array containing two Flight objects was retrieved. As expected, the two objects are mem_atl and mem_lex from the above object diagram. Be aware that has_many-generated methods, like departure_flights, will always return an array of objects (versus a single object like belongs_to-generated methods).

Given that my_flight_array references an array, we can process each object in the array using an each loop. For example, print the departure time for each flight in the array by entering this Ruby code into the console:

my_flight_array.each do |f|
  puts 'DEPARTURE TIME: ' + f.departure_time.to_s
end

Note that the departure time for each Flight object in the my_flight_array array is printed.

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


❮ Back Next ❯