I'm trying to create a relationship between addresses and trips, and I'm not sure exactly how to set up the relationship.
Each trip will have two addresses: the starting address and the ending address. Addresses can be used in many different trips, and they can either be the starting address or the ending address depending on the trip. What I envision is that when users are creating a new trip, they can select from a dropdown of all their addresses, so they could make a trip from their address called, say, "home" to their address called, say, "airport."
There is already a polymorphic relationship set up between addresses (as locatable) with some other models in the app, but in this case the same address needs to belong to two different models (the user and the trip). Would a polymorphic join table be a good solution? Even if that did solve the problem, once you had connected two different addresses to trips, what's the best way to distinguish the starting address from the ending address?
Thanks for any suggestions!
Edit:
I have implemented everything below by hakunin, but I still cannot find a way to make the feature function. I decided to use fields_for
to build the TripLocation
objects for each Trip
, but I cannot figure out what to put in the controller. When I put:
def new
@trip = Trip.new
@trip.origin_trip_location.build
@trip.destination_trip_location.build
end
I get the error undefined method build for nil:NilClass
. I was thinking of using just @trip.trip_location.build
instead but then I get the error undefined method trip_locations for #<Trip:0x007f5a847f94b0>
because in the model for Trip does not say has_many :trip_locations
. By just using regular has_many :trip_locations
I have been able to enter in all the information necessary into the join table just using the form helper fields_for :trip_locations
and saying that a Trip has_many :trip_locations
, but then I have no method to query and find which address has the boolean in the join table set as true and which one has it set as false. If I could just get that problem solved, I would be all set, I think.
In rails this is normally done with a condition on the association. You can use it in combination with "has_one through". Create a new model, let's call it TripLocation
which would be the mapping table between trips and addresses. In it you would have the column, say "destination". If the column is true, this mapping is for a destination address.
So let's say the migration looks like this:
create_table :trip_locations do |t|
t.belongs_to :trip
t.belongs_to :address
t.boolean :destination
end
And these would be the models:
class TripLocation < ActiveRecord::Base
belongs_to :trip
belongs_to :address
end
class Trip < ActiveRecord::Base
has_one :origin_trip_location,
class_name: 'TripLocation',
conditions: { destination: nil }
has_one :destination_trip_location,
class_name: 'TripLocation',
conditions: { destination: true }
has_one :origin, through: origin_trip_location, source: :trip
has_one :destination, through: destination_trip_location, source: :trip
end
So because of the conditions set on the "through" associations, calling @trip.origin
and @trip.destination
should give you the correct addresses.
When it comes to designating addresses as origin or destination, you can simply assign an address to whichever you need. @trip.origin = Address.first
, or @trip.destination = Address.second
, and I believe it should do the right thing with setting the destination flag. Try it.