Q:

ember.js, how to set relationships in newly created records

I'd like to set the relationship between two records, both newly created: I create country, I create city, then set that country hasMany city and conversely city belongsTo country.  From the ember "Models guide"

you cannot assign a promise as a relationship [...] However, you can easily set the relationship after the promise has fulfilled:

var store = this.store;

var post = store.createRecord('post', {
  title: 'Rails is Omakase',
  body: 'Lorem ipsum'
});

store.find('user', 1).then(function(user) {
  post.set('author', user);
});

The above example doesn't exactly fit my case since I wouldn't know the id of the user record, which in the snippet is assumed to be 1.  My example is more like
App. Country = DS.Model.extend({
  name: DS.attr('string'),
  cities: DS.hasMany('city', {async: true})
});

App. City = DS.Model.extend({
  name: DS.attr('string'),
  country: DS.belongsTo('country', {async: true})
});

then, when manipulating records, I'd like to create and link all in one go:
var store = this.store;

var country = store.createRecord('country', {
    name: 'France'
});

var city = store.createRecord('city', {
    name: 'Paris'
});

// How to get the 'foo' created just above out of the store?  What id? 
store.find('city', ??) .then(function(city) {
    country.set('cities', [city]);
});

// Same story, what's the id of the country I just created? 
store.find('country', ??) .then(function(country) {
    city.set('country', country);
});

question is: what to put in th ?? s in my snippet above. 

A:

The way i would implement it -
var store = this.store;

var onCountrySuccess = function(resolvedCountry){
resolvedCountry.get('id');
};

var onCountryFailure = function() {} ;

var onSuccessfulCityCreate = function(resolvedCity){
   store.createRecord('country', {
    name: 'France',
    cities : [resolvedCity]
 }.save().then(onCountrySuccess, onCountryFailure):

var onCityFailure = function(){ alert("Failed") });

var city = store.createRecord('city', {
    title: 'Paris'
}).save().then(onSuccessfulCityCreate, onCityFailure);

And If you had a set of Cities 
var resolve = function(resolvedRecords) {
 store.createRecord('country', {
    name: 'France',
    cities : resolvedRecords
 }.save();
}:
var reject = function(){}; 

var citiesToSave = someCityContainingArray.map(function(city){
 return city.save();
});

Ember. RSVP.all(citiesToSave).then(resolve, reject);

