# Polymorphic associations on models with many names

**URL:** <https://www.ruby-forum.com/t/polymorphic-associations-on-models-with-many-names/134926>\
**Category:** Rails\
**Created:** [April 16, 2008, 7:21am UTC](https://www.ruby-forum.com/t/polymorphic-associations-on-models-with-many-names/134926 "2008-04-16T07:21:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f6c823/32.png) [@harry](https://www.ruby-forum.com/u/harry)\
**Post date:** [April 16, 2008, 7:21am UTC](https://www.ruby-forum.com/t/polymorphic-associations-on-models-with-many-names/134926/1 "2008-04-16T07:21:38Z")

</div>

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
