0

In my Rails app I'll have the following relationships:

class Location < ActiveRecord::Base; end

class Park < Location; end class Campground < Location; end class Trails < Location; end

My goal is to be able to link all of specific types of locations together. For example, if I select a park, I would like to be able to see all the campgrounds and trails that are related to the park. Likewise, if I were to select a Trail or a Campground, I would like to be able to find the other types of locations that are related to the Trail or Campground.

Any ideas how I might achieve this?

Thanks for looking

jklina
  • 3,407
  • 27
  • 42

1 Answers1

1

Depending on how you want them to be related, you will probably need a separate relationship/table to store that information. For example, you could do something like this:

class Location < ActiveRecord::Base
  has_and_belongs_to_many :location_groups
end

class Park < Location; end class Campground < Location; end class Trails < Location; end

class LocationGroup < ActiveRecord::Base
  has_and_belongs_to_many :locations
end

Then to find other "locations" in the same location group (of which it could belong to several):

@park = Park.first
@group = @park.location_groups.first
@group.locations # All locations in that group

Hope that helps give you some ideas!

Chris Cherry
  • 28,118
  • 6
  • 68
  • 71