title: You cannot call create unless the parent is saved
body: <p>I have an iteration </p>
  
  <pre><code>      current_item = line_items.create(product_id: product_id, instruction: instruction)
        current_item.save
        attribute_values.each do |attribute|
          current_item.line_item_attributes.create(product_attribute_id: attribute)
        end
  </code></pre>
  
  <p>and it gives me an error on creating the attributes. When I print out <code>current_item</code> I get </p>
  
  <pre><code>#&lt;LineItem id: nil, product_id: 1, cart_id: 36, created_at: nil, updated_at: nil, quantity: 1, instruction: "", order_id: nil&gt;
  </code></pre>
  
  <p>which from my understanding means it's not saved yet. How can I fix this?</p>
  
  <p>After calling <code>create!</code> the error returned is:</p>
  
  <pre><code>Validation failed: Order must exist
  </code></pre>
  
  <p>the Order model is recently added with this migration:</p>
  
  <pre><code>class AddOrderToLineItem &lt; ActiveRecord::Migration[5.0]
    def change
      add_reference :line_items, :order, index: true, foreign_key: true
    end
  end
  </code></pre>
  
  <p>and <code>LineItem belongs_to :order</code>. The issue here is I want it to be blank, because in this case it doesn't belong to an order yet.</p>