Newly created record automatically assigned to belongsTo relation


#1

If have a person model with a belongsTo-relation to the last measurement.

export default DS.Model.extend({
  lastMeasurement: DS.belongsTo('measurement')
});

Then I create a new measurement in the route:

var w = this.store.createRecord('measurement', {
  person: this.get('person'),
  date: new Date()
});

The new measurement is passed to a form and saved on submit.

The problem is that the newly created measurement is automatically assigned to the lastMeasurement property of the person when created, no matter what. I want this to be resolved by the server, to check if the measurement is indeed the most recent and if so assign it to person.lastMeasurement. Not automatically whenever I create a new record.

I feel like Ember Data tries to resolve something that’s not conform my idea of how Ember Data should work.

Is there a solution to this?


#2

If you successfully create the record and it is truly a belongsTo relationship then it should be the case that the persons lastMeasure is the one you just created. If other measures are being created that the client is unaware of you either have to refresh your person model whenever you want to check or have the server push back data to the client.

If it’s on measurement create that you’re concerned about the persons last measure getitng out of sync you could sideload the person when you create the measurement.


#3

Actually the date of the measurement can be changed in the form, so I can report a measurement that has been done yesterday. To clearify, my question is not related to the functionality of my application but to the fact that Ember Data automatically assigns the newly created measurement to the belongsTo property of the person. It seems like Ember Data is thinking “well, person has a belongsTo relation to measurement, and measurement a belongsTo relation to person, so yeah, let me link them to each other immediately”.


#4

I’m not 100% I’m understanding your situation fully, but it seems like maybe what you would want is person hasMany measures and then have a computed property or just a separate property set by the server that is the lastMeasure?

The way that ember data is filling the record makes sense because you’re telling ember-data that a person can have only one measurement and you just made a measurement connected to that person so why wouldn’t it be the case that it’s connected? Or perhaps I’m missing something.