Reputation: 626
I have the following models:
reference
used for listing valuesuser
modelNow, user
can have many departments
whose model is exactly reference
.
How can I declare in user
model that the departments
can have many reference
objects?
I tried the following but can't make it work:
import DS from 'ember-data';
import Ember from 'ember';
export default DS.Model.extend({
name: DS.attr('string'),
departments: DS.hasMany('reference')
});
For what it's worth, this question stemmed from needing to include the id in the POST payload using Ember Data. My original user
model is like this:
import DS from 'ember-data';
import Ember from 'ember';
export default DS.Model.extend({
name: DS.attr('string'),
departments: DS.attr()
});
Problem with above model, whenever I save a record, it doesn't include the ID of the references in the payload even though they're already loaded in the store.
Do I need to create a custom serializer for the user
model?
Upvotes: 0
Views: 439
Reputation: 760
It sounds like you are after the EmbeddedRecordsMixin.
With it you can tell the serializer to include the entire model or its' id. Something like this:
//serializer/user.js
import DS from 'ember-data';
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
departments: { embedded: 'always' }
}
});
Upvotes: 1