Reputation: 33
I have the following structure
class Country < ApplicationRecord
has_many :cities
end
class City < ApplicationRecord
belongs_to :country
has_and_belongs_to_many :tours
end
class Tour < ApplicationRecord
has_and_belongs_to_many :cities
end
Now I want something like
Tour.first.countries
Country.first.tours
What is the best way to achieve that without creating an additional join table for countries_tours
Upvotes: 1
Views: 27
Reputation: 33
class Country < ApplicationRecord
has_many :cities
has_many :tours, -> { distinct }, through: :cities
end
class Tour < ApplicationRecord
has_and_belongs_to_many :cities
has_many :countries, -> { distinct }, through: :cities
end
I managed it
Upvotes: 1