Call save on relation from within a record


#1

What I’m trying to do is call save on a belongsTo relation from within a record. Let’s say I have a person with a name relation. I’ve added the next method to person model:

extendedSave() {

  // Check for name to save.
  if (this.get('name.hasDirtyAttributes')) { // true
    console.log(this.get('name').save); // undefined
    this.get('name').save(); // error
    this.name.save(); // doesn't work either
  }

  // Check for phone numbers to save.
  this.get('phoneNumbers').forEach(function(phoneNumber) {
    if (phoneNumber.get('hasDirtyAttributes')) {
      phoneNumber.save(); // work like a charm
    }
  });
  
}

As you can see, looping through a hasMany relation (phoneNumbers) and calling save on any of them causes no problem. But apparently there’s no save method on the belongsTo relation.

What am I doing wrong here?


#2

Found it. Apparently this.get('name') returns a promise. So this works:

this.get('name').then(function(name) {
  name.save();
});