title: Rails Formtastic nested form - form for attributes from join model
body: <p>The problem I am facing with <code>Formtastic</code> is that I have a Form to create a new <code>Order</code>. In this form I want to select multiple existing <code>Food</code> items from a list. These should be added to the new Order I am submitting. At the same time I also want to set attributes in the <code>FoodOrder</code> join model. This Model has an integer quantity attribute for which i would like to have a field in my form. </p>
  
  <p>What I am basically looking for is a form that lists all Food Items and puts a field for the <code>quantity</code> on the same line as the Food Item it belongs to.</p>
  
  <p>The Models</p>
  
  <pre><code>class Order &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :restaurant
    has_many :food_orders
    has_many :foods, :through =&gt; :food_orders
  end
  
  class FoodOrder &lt; ActiveRecord::Base
    belongs_to :food
    belongs_to :order
  end
  
  class Food &lt; ActiveRecord::Base
    has_many :food_orders
    has_many :orders, :through =&gt; :food_orders
    belongs_to :category
  end
  </code></pre>
  
  <p>This is one of the versions of the form I have tried so far. But I am just baffled and do not know how to get fields for the FoodOrder Model.</p>
  
  <pre><code>&lt;%= semantic_form_for [@restaurant, @order] do |f| %&gt;
    &lt;%= f.inputs do %&gt;
      &lt;%= f.input :comment %&gt;&lt;br /&gt;
      &lt;%= f.input :table_id %&gt;&lt;br /&gt;
      &lt;%# &lt;%= f.input :foods, :as =&gt; :check_boxes %&gt;
      &lt;%= f.inputs :for =&gt; :foods do |food| %&gt;
        &lt;%= food %&gt;
        &lt;%= food.inputs :quantity %&gt;
      &lt;% end %&gt;
    &lt;% end %&gt;
    &lt;%= f.buttons do %&gt;
      &lt;%= f.commit_button %&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>My models have these attributes</p>
  
  <pre><code>create_table "food_orders", :force =&gt; true do |t|
    t.integer  "quantity",   :null =&gt; false
    t.decimal  "price",      :null =&gt; false
    t.integer  "food_id",    :null =&gt; false
    t.integer  "order_id",   :null =&gt; false
    t.text     "comment"
    t.datetime "created_at", :null =&gt; false
    t.datetime "updated_at", :null =&gt; false
  end
  
  create_table "foods", :force =&gt; true do |t|
    t.integer  "category_id",                     :null =&gt; false
    t.string   "name",                            :null =&gt; false
    t.string   "description"
    t.string   "image"
    t.decimal  "default_price",                   :null =&gt; false
    t.boolean  "active",        :default =&gt; true, :null =&gt; false
    t.datetime "created_at",                      :null =&gt; false
    t.datetime "updated_at",                      :null =&gt; false
  end
  
  create_table "orders", :force =&gt; true do |t|
    t.integer  "restaurant_id",                    :null =&gt; false
    t.integer  "user_id",                          :null =&gt; false
    t.integer  "table_id",                         :null =&gt; false
    t.decimal  "total",                            :null =&gt; false
    t.datetime "finished_at"
    t.datetime "created_at",                       :null =&gt; false
    t.datetime "updated_at",                       :null =&gt; false
  end
  </code></pre>