samedi 9 juillet 2016

hasMany association in Ember js

app/models/card.js

export default Model.extend({
  title: attr('string'),
  description: attr('string'),
  users: hasMany('user')
});

app/models/user.js

export default Model.extend({
  email: attr('string'),
  name: attr('string')
});

app/routes/card/new.js

actions: {
  save(title, description) {
    const newCard = this.get('store').createRecord('card', {
      title,
      description
    });

    this.get('store').findRecord('user', 1).then(function(user) {
      newCard.set('users', user);
      console.log(newCard);
    });

    newCard.save().then((card) => {
      // go to the new item's route after creating it.
      this.transitionTo('card.card', card);
    });
  }
}

So, when I'm saving a card, it is throwing me this error:

Assertion Failed: You must pass an array of records to set a hasMany relationship.

I want to create an association between the newly created card and the user.

Other info:

Repo link: http://ift.tt/1Xv7Dye

Ember : 2.6.1

Ember Data : 2.6.1

Aucun commentaire:

Enregistrer un commentaire