Ember data - add object to hasMany

I have an issue with ember-data and adding objects to a hasMany relationship.

I am using the JSONAPI adapter and serializer.

var model = this.store.peekRecord("model", "id");
var related = this.store.peekRecord("related-model", "id");
model.get("relationField").then(function(relations) {
  relations.addObject(related);
  model.save();
});

The generated request does not contain any relationship attributes, neither the currently existing ones nor the newly added one.

{"data":{"type":"user_roles","id":"admin","attributes":{"name":"admin"}}}

Any hints?

I had a similar problem and found that my model did actually include the reference, it just wasn’t being output in the generated request by the serializer. There’s a solution in this Github issue.