title: Creating a deeply nested object in Rails
body: <p>I've built a small API that, when posted a JSON object, creates the representative model records. The data looks like this:</p>
  
  <pre><code>{
    "customer": {
      "email": "michael@myemail.com",
      "first_name": "Michael T. Smith",
      "last_name": "",
      "shipping_address_1": "",
      "telephone": "5551211212",
      "source": "Purchase"
    },
    "order": {
      "system_order_id": "1070",
      "shipping_address_1": "",
      "billing_address_1": "123 Your Street",
      "shipping": "0",
      "tax": "0",
      "total": "299",
      "invoice_date": 1321157461,
      "status": "PROCESSING",
      "additional_specs": "This is my info!",
      "line_items": [
        {
          "quantity": "1",
          "price": "239",
          "product": "Thing A",
          "comments": "comments"
          "specification": {
            "width": "12",
            "length": "12",
          },
        },
        {
          "quantity": "1",
          "price": "239",
          "product": "Thing A",
          "comments": "comments"
          "specification": {
            "width": "12",
            "length": "12",
          },
        },
      ]
    }
  }
  </code></pre>
  
  <p>The question is how to create the nested objects. My models are setup as such:</p>
  
  <pre><code>class Order &lt; ActiveRecord::Base
    has_many :line_items
    belongs_to :customer
  
    accepts_nested_attributes_for :line_items
  end
  
  class LineItem &lt; ActiveRecord::Base
    belongs_to :order
    has_many :specifications
  end
  
  class Specification &lt; ActiveRecord::Base
    belongs_to :LineItem 
  end
  </code></pre>
  
  <p>I'm trying to create the records using this code:</p>
  
  <pre><code>@order = @customer.orders.build(@data[:order])
  @order.save
  </code></pre>
  
  <p>Is there a better way to do this? Currently I'm getting this error: <code>ActiveRecord::AssociationTypeMismatch in ApiController#purchase_request LineItem(#70310607516240) expected, got Hash(#70310854628220)</code></p>
  
  <p>Thanks!</p>