title: Saving multiple records on one create form dependign on associated items
body: <p>Goal: To create multiple Orders based on how many of a certain associated records are present.</p>
  
  <p>Here's the form: (not important but receiving errors</p>
  
  <pre><code>&lt;%= form_for(@order) do |form| %&gt;
      &lt;%= form.hidden_field :cart_id, value: @cart.id %&gt;
      &lt;%= form.hidden_field :store_order_id, value: "1" %&gt;
      &lt;%= form.text_field :shipping_name %&gt;
      &lt;%= form.text_field :shipping_address1 %&gt;
    &lt;div class="actions"&gt;
      &lt;%= form.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>ActionView::Template::Error (First argument in form cannot contain nil
    or be empty):</p>
  </blockquote>
  
  <p>Models:</p>
  
  <p>ShopProduct</p>
  
  <pre><code>has_one :order
  has_many :line_items
  </code></pre>
  
  <p>Order</p>
  
  <pre><code>belongs_to :product, optional: true
  belongs_to :shop_product, optional: true
  belongs_to :cart
  </code></pre>
  
  <p>LineItem</p>
  
  <pre><code>belongs_to :shop_product, optional: true
  belongs_to :product, optional: true
  belongs_to :cart
  </code></pre>
  
  <p>Cart</p>
  
  <pre><code>has_many :line_items, dependent: :destroy
  has_one :order
  </code></pre>
  
  <p>OrdersController:</p>
  
  <pre><code>  def create
      cart = Cart.find(params["order"]["cart_id"])
      line_items = LineItem.where(cart_id: cart)
      shop_product_line_items = line_items.where.not(shop_product: [nil, ""])
      product_line_items = line_items.where.not(product: [nil, ""])
      shop_products = ShopProduct.find(shop_product_line_items.each { |li| li.shop_product })
      products = Product.find(product_line_items.each { |li| li.product })
  
      if shop_products.present?
        shop_products.each { |sp|
        @order = Order.new
        @order.shop_product_id = sp.id
        @order.product_id = sp.product_id
        @order.cart_id = cart
        @order.save!
      }
      end
  
      if products.present?
        products.each { |p|
        @order = Order.new
        @order.product_id = p.id
        @order.cart_id = cart
        @order.save!
      }
      end
  
      @orders = Order.where(cart_id: cart)
  
      respond_to do |format|
        if @orders.count == line_items.count
          session[:cart_id] = nil
          format.html { redirect_to @order, notice: 'Order was successfully created.' }
          format.json { render :show, status: :created, location: @order }
        else
          format.html { render :new }
          format.json { render json: @order.errors, status: :unprocessable_entity }
        end
      end
    end
  </code></pre>
  
  <p>How it works is there is a cart which then is attached to many <code>line_items</code> which can be <code>shop_product</code> or <code>product</code>.  I then want to save a separate order based on each <code>@cart.line_items</code>.</p>
  
  <p>How can I create multiple Order's based on how many line_items are present in the cart?</p>