title: Polymorphism and forms in Ruby on Rails
body: <p>I've been full of questions lately, but thanks to this awesome community, I'm learning a ton.</p>
  
  <p>I got all the help I needed with polymorphic associations earlier and now I have a question about tackling forms with polymorphic models.  For instance I have Phoneable and User, so when I create my form to register a user, I want to be able to assign a few phone numbers to the user (ie: cell, work, home).</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :phones, :as =&gt; :phoneable
  end
  
  class Phone &lt; ActiveRecord::Base
    belongs_to :phoneable, :polymorphic =&gt; true
  end
  
  class CreateUsers &lt; ActiveRecord::Migration
    t.column :name, :string
    t.references :phoneable, :polymorphic =&gt; true
  end
  
  class CreatePhones &lt; ActiveRecord::Migration
    t.column :area_code, :integer
    t.column :number, :integer
    t.column :type, :string
    t.references :phoneable, :polymorphic =&gt; true
  end
  </code></pre>
  
  <p>Now when I create my form, I'm getting confused.  Normally I would do the following:</p>
  
  <pre><code>- form_for :user, :html =&gt; {:multipart =&gt; true} do |form|
    %fieldset
      %label{:for =&gt; "name"} Name:
      = form.text_field :name
      ##now how do I go about adding a phone number?
      ##typically I'd do this:
      ##%label{:for =&gt; "phone"} Phone:
      ##= form.text_field :phone
  </code></pre>
  
  <p>Using polymorphism, would I just approach the same way, but use fields_for?</p>
  
  <pre><code>- user_form.fields_for :phone do |phone| %&gt;
    %label{for =&gt; "area_code"} Area Code:
    = phone.text_field :area_code
    %label{for =&gt; "number"} Number:
    = phone.text_field :number
  </code></pre>
  
  <p>Is this the correct approach in this instance?</p>