I have three models: Trip, Visit, Park
The relationships are currently set up like:
class Trip < ApplicationRecord
has_many :visits
end
class Visit < ActiveRecord::Base
belongs_to :park, counter_cache: true
belongs_to :trip, optional: true
end
class Park < ActiveRecord::Base
has_many :visits
end
Basically, If I have a trip, what I want to return is a list of Parks visited on that trip.
I could iterate the visits and grab the parks and deduplicate but there's most likely a more "railsy" way to achieve this that I just don't know about.
Any suggestions?
Many thanks.
question from:
https://stackoverflow.com/questions/65876413/accessing-a-relationship-through-another-relationship 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…