Polymorphic associations on models with many names

Hi,

I’ve got Itineraries, Stops (join model), and Locations. I’d like to
be able to do Itinerary.locations.find_by_name(:name) but Locations
can have one of several types (Museum, Restaurant, Bar, Train Station,
etc) and each one of those can have several names referring to the
same instance (“Manny’s Bar”, “Manny’s”, “The Bar Formerly Known as
Manny’s”, “The New Roadhouse”).

Is there a way to make it work? How do I find_by_name when I don’t
know which type of model the name refers to?

Example

class Itineraries
has_many :stops
has_many :locations, :through => :stops

class Stops
belongs_to :itineraries
belongs_to :locations

class Locations
has_many :stops
has_many :itineraries, :through => :stops
belongs_to :locationable, :polymorphic => true

class Museum
has_one :location, :as => :locationable

class Bar
has_one :location, :as => :locationable

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs