@ghoshnirmalya wrote:
I asked the question on StackOverflow (http://stackoverflow.com/questions/38289037/hasmany-association-in-ember-js/38289732#38289732) but didn't get much help. So, I'm asking it here:
// 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'), cards: hasMany('card') }); // 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 tried doing
newCard.set('users', [user])
but the response is{ "data":{ "attributes":{ "title":"sad", "description":"asdasd" }, "relationships":{ "users":{ "data":[ ] } }, "type":"cards" } }
where the
users.data
is a blank array. My backend expects theusers.data
to be something like[1, 2, 3]
or even[1]
.I want to create an association between the newly created card and the user.
Other info:
Repo link: https://github.com/ghoshnirmalya/hub-client
Ember : 2.6.1
Ember Data : 2.6.1
Posts: 1
Participants: 1