Pushing has many records into a new model

Hi everyone,

I am developping a small emberjs application. I have 2 models, one represents people, the second one represents trips.

import DS from 'ember-data';

var attr = DS.attr;

export default DS.Model.extend({
  
    firstname: attr("string"),
    lastname: attr("string"),
    trips: DS.hasMany('trip')
})

and

import DS from 'ember-data';

var attr = DS.attr;

export default DS.Model.extend({
    date: attr("string"),
    location: attr("string"),   
});

I have a “new” view to create a new person. In this view, have a button with an action “addTrip”.

addTrip: function(date, location, reason) {
    var trip = this.store.createRecord("trip", {
    date: date,
        location: location,
    });
    trip.save();
    this.get('trips').pushObject(trip); 
}

But this is throws me an error. I read this topic: createRecord that has hasMany relationship

Is it possible to add relationships to a being created model, here, for example, the person model?

Thanks for your help.

Michael

Since I found this on google while trying to solve the same problem, I am posting a solution on this old topic. In Ember 2.10 this works by adding a belongsTo on the trip model:

import DS from 'ember-data';
export default DS.Model.extend({
  ....
  person: DS.belongsTo('person')
});
1 Like