Reputation: 3938
I have a FeedPost
model that has_many :flights
. Flights then have an origin_id and a destination_id, which both map to a locations
table.
The question, is is there a rails way I can call FeedPost.first.locations
and get all locations for all flights associated with that FeedPost
?
Doing this:
has_many :locations, through: :flights, source: :origin
has_many :locations, through: :flights, source: :destination
Just returns the destination locations.
I've seen a lot of similar questions, but they all relate to getting the values of each column, not both columns in one call.
Upvotes: 1
Views: 108
Reputation: 3005
This is what jvillian commented, which is in fact an answer
class FeedPost < ApplicationRecord
has_many :flights
has_many :destinations, through: :flights, source: :destination
has_many :origins, through: :flights, source: :origin
def locations
(origins + destinations).uniq
end
Upvotes: 2