title: Rails Polymorphic has_many
body: <p>Using Ruby on Rails, how can I achieve a polymorphic <code>has_many</code> relationship where the owner is always of a known but the items in the association will be of some polymorphic (but homogenous) type, specified by a column in the owner?  For example, suppose the <code>Producer</code> class <code>has_many</code> products but producer instances might actually have many Bicycles, or Popsicles, or Shoelaces.  I can easily have each product class (Bicycle, Popsicle, etc.) have a <code>belongs_to</code> relationship to a Producer but given a producer instance how can I get the collection of products if they are of varying types (per producer instance)?</p>
  
  <p>Rails polymorphic associations allow producers to belong to many products, but I need the relationship to be the other way around.  For example:</p>
  
  <pre><code>class Bicycle &lt; ActiveRecord::Base
    belongs_to :producer
  end
  
  class Popsicle &lt; ActiveRecord::Base
    belongs_to :producer
  end
  
  class Producer &lt; ActiveRecord::Base
    has_many :products, :polymorphic_column =&gt; :type # last part is made-up...
  end
  </code></pre>
  
  <p>So my Producer table already has a "type" column which corresponds to some product class (e.g. Bicycle, Popsicle, etc.) but how can I get Rails to let me do something like:</p>
  
  <pre><code>&gt;&gt; bike_producer.products
  #=&gt; [Bicycle@123, Bicycle@456, ...]
  &gt;&gt; popsicle_producer.products
  #=&gt; [Popsicle@321, Popsicle@654, ...]
  </code></pre>
  
  <p>Sorry if this is obvious or a common repeat; I'm having surprising difficulty achieving it easily.</p>