Clearing async belongsTo relationships

Hi All,

I’m trying to find the best way to clear an async relationship, defined as:

LogEntry = DS.Model.extend({
    location: DS.belongsTo('location', {async: true}),
)}

I have a HBS template with a drop down that allows users to pick from a list of all locations or select a prompt of “------” which I can see sets the value to null on the client. However, when the user clicks submit, the serializeBelongsTo method in ember-data executes the following code which is causing me issues:

if (isNone(belongsTo)) {
    json[key] = belongsTo;
} else {
    json[key] = get(belongsTo, 'id');
}

The get(belongsTo, ‘id’) call returns undefined and therefore is not included in the JSON PUT request to the server making it impossible for users to clear the location field. I have worked around this issue by overriding serializeBelongsTo as follows:

DS.JSONSerializer.reopen({
  serializeBelongsTo: function(record, json, relationship) {
    var key = relationship.key;
    var belongsTo = get(record, key);
    var get = Ember.get
    var isNone = Ember.isNone;

    key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo") : key;

    if (isNone(belongsTo)) {
      json[key] = belongsTo;
    } else if (get(belongsTo, 'id')) {
      json[key] = get(belongsTo, 'id');
    } else {
      json[key] = null;
    }

    if (relationship.options.polymorphic) {
      this.serializePolymorphicType(record, json, relationship);
    }
  }
})

However, I’m concerned this will cause regressions and/or undesired data loss in some cases. I have found several other related issues:

I’m still not sure what the best way to accomplish this desired behaviour is. The weird thing is, it works fine without the workaround when the user selects a location other than null. Is there a recommended workaround? Is this an ember-data bug?

Thanks!