title: Best Practice for Creating New Record with belongsTo Relationship
body: <p>I am wondering about the best practice for creating a new record in Ember with <code>createRecord()</code> and then persisting it to the API?  Specifically, should Ember's <code>POST</code> request generally be a single JSON that embeds all the model's relationships, or is it customary to <code>POST</code> each relationship individually?</p>
  
  <p>In my code, I'm not able to get a single JSON, so I'm wondering if I'm missing the "Ember Way" or (more likely) I have a mistake in my code?</p>
  
  <p><strong>DETAILS:</strong></p>
  
  <p>Here are the details of my setup. I have two models:</p>
  
  <p><strong>/models/OrgUser.js:</strong>  </p>
  
  <pre><code>DS.Model.extend({
      ...
      orgPerson: DS.belongsTo('org-person', { inverse: 'org-user', async: true, embedded: 'always' }),
  });
  </code></pre>
  
  <p><strong>/models/OrgPerson.js:</strong></p>
  
  <pre><code>DS.Model.extend({
     ...
     orgUser: DS.belongsTo('org-user'),
  })
  </code></pre>
  
  <p>I'm attempting to create a new user on the "Create New User" page.  The route for that page is below. Is this the best place to call <code>createRecord()</code> for my new models?</p>
  
  <p><strong>/routes/org-users/add.js:</strong></p>
  
  <pre><code>Ember.Route.extend({
      model: function() {
          var orgPerson = this.store.createRecord('org-person');
          var orgUser = this.store.createRecord('org-user' );
          orgUser.set('orgPerson', orgPerson);
  
          return orgUser;
      },
       ...
  }
  </code></pre>
  
  <p>Using Chrome console to look at the orgUser object after I call <code>set</code> shows no evidence at all that I have added anything to <code>orgUser</code>. The "Ember" tab of Chrome Debug Tools does reflect the relationship, though.</p>
  
  <p>On my "Create New User" page, my input fields all correspond to both <code>OrgUser</code> properties and <code>OrgUser.OrgPerson</code> properties.  Here's an example:</p>
  
  <p><strong>/templates/org-users/add.hbs</strong></p>
  
  <pre><code>...
  {{input value=username}} // a property of OrgUser
  {{input value=orgPerson.firstName}} // a property of OrgUser.orgPerson
  ...
  </code></pre>
  
  <p>In my route, when I go to <code>save()</code> Ember Data <code>POST</code>s only the orgUser JSON with a <code>null</code> value for <code>orgPerson.</code> I'd like it to embed the <code>orgPerson</code> serialized object in the <code>orgPerson</code> property.</p>
  
  <p><strong>/routes/org-users/add.js:</strong></p>
  
  <pre><code>Ember.Route.extend({
      ...
      actions: {
          submitForm: function() {
              ...
              this.currentModel.save().then( onSuccess ).catch( onFailure );
              ...
          }
      }
  });
  </code></pre>
  
  <p>This results in a <code>POST</code> request with the following body:</p>
  
  <pre><code>{
  "orgUser":{
     "username":"MyUsername",
     "orgPerson":null,
     "id":null
  }
  </code></pre>
  
  <p>Note that <code>orgPerson</code> is null.  Thanks in advance for any assistance!</p>
  
  <p><strong>UPDATE:</strong> Once again, I think I will need to take a fresh look at my serializer.  Here's how it's currently defined.</p>
  
  <p><strong>/serializers/application.js:</strong></p>
  
  <pre><code>DS.RESTSerializer.extend({
  
      // Use the default approach to serializing, but add the id property
      serialize: function(record, options) {
          var json = this._super.apply(this, arguments);
          json.id = record.id;
          return json;
      },
  
      serializeBelongsTo: function(record, json, relationship) {
      var key = relationship.key;
        key = this.keyForRelationship ? this.keyForRelationship(key, 'belongsTo') : key;
  
        var data = record.get('data');
  
        if (relationship.options.embedded &amp;&amp; relationship.options.embedded === 'always') {
          json[key] = data[relationship.key] ? data[relationship.key].serialize( { includeId: true } ) : null;
          }
        else {
              json[key] = data[relationship.key] ? data[relationship.key].get('id') : null;
          }
  
          if (relationship.options.polymorphic) {
              this.serializePolymorphicType(record, json, relationship);
          }
      }
  });
  </code></pre>
  
  <p>Per @Kingpin2k's comment, there appears to be some ambiguity (and bugs!) on how best to handle <code>serialize()</code> for a <code>belongsTo</code> relationship. My serializer customization above works great for records that are obtained through <code>this.store.find()</code>, but now I need to enable them for <code>createRecord()</code>.  Additional suggestions, pointers are welcome!</p>