krn
krn

Reputation: 6815

Rails ActiveRecord associations

I have the routes table with fields from_airport_id and to_airport_id.

Let's say:

route = Route.find(1)

The question: how to define the ActiveRecord associations, that route.from_airport would be equal to Airport.find(route.from_airport_id) and route.to_airport = Airport.find(route.to_airport_id)?

In other words:

from_airport_id => airport.id

to_airport_id => airport.id

I guess, the query should be:

route = Route.find(1).includes(:airports)

But how do I select two records from the same table at once?

Upvotes: 0

Views: 117

Answers (1)

krunal shah
krunal shah

Reputation: 16339

has_many :from_airports, :class_name => "AirPort",
:foreign_key => "from_airport_id"


has_many :to_airports, :class_name => "AirPort",
:foreign_key => "to_airport_id"

Upvotes: 1

Related Questions