title: Saving nested JSON object to database
body: <p>Lets say I have a model.</p>
  
  <p>Passengers belongs to Flights.
  Flights belongs to Trips</p>
  
  <pre><code>class Trip &lt; ActiveRecord::Base    
      has_many :flights, :dependent =&gt; :destroy, :order =&gt; "order_num ASC"    
  end
  
  
  class Flight &lt; ActiveRecord::Base
      belongs_to :trip, touch: true
      has_many :passengers, :dependent =&gt; :destroy
  end
  
  class Passenger &lt; ActiveRecord::Base
      belongs_to :flight, touch: true
  end
  </code></pre>
  
  <p>And I'm getting this sent back to the rails app.  (when the user calls save).</p>
  
  <p>*The top level is the trip</p>
  
  <pre><code>{    
      name: 'Hello Trip',
      date: '2013-08-12',
      flights: [
          {
              id: 1
              depart_airport: 'RDU',
              arrive_airport: 'RDU',
              passengers: [
                  {
                      user_id: 1,
                      request: true
                  }
              ]
          },
  
          {
  
              depart_airport: 'RDU',
              arrive_airport: 'RDU',
              passengers: [
                  {
                      user_id: 1,
                      request: true
                  },
                  {
                      user_id: 2
                      request:true
                  }
              ]
          }
  
      ]
  }
  </code></pre>
  
  <p>Right now I'm getting the saved json in and manually looping through the flights to see if there is an id.  If there is i'm updating it.  If not I'm creating a new one.  Then adding the passengers.</p>
  
  <p>I'm wondering if there is an automatic format that Rails takes that can do all the saving for me.  I know when you submit a nested form it creates a similar pattern, and adds a _destroy property and the id is a timestamp if it's just created.  Would the JSON saving be similar to that? </p>
  
  <p>Thanks for any help!</p>