northben
northben

Reputation: 5588

How to create an object in a one to many relationship

I have parents and children. As you'd expect, a Parent 'hasMany' Child objects. I am able to create a new Parent just fine, but I can not get new Child objects to show up on my template. According to the Chrome and Firefox Ember debug tool, it looks like the Child objects are being created in the store correctly. But the new children never appear in the template.

I believe I've followed the Ember docs to the letter, and yet I can not get my code to work.

I've created the following jsbin to demonstrate my situation. http://emberjs.jsbin.com/zodorule

How do I create new many-related objects in Ember?

Upvotes: 4

Views: 1223

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

Ember Data isn't a database, so setting the relationship from one side won't automatically hook it up from the other.

  var child = this.store.createRecord('Child', {name: 'Fred'});
  this.store.find('parent', 1).then(function(parent) {
    child.set('parent', parent);
    parent.get('children').then(function(children){
      children.pushObject(child);
    });
  });

Example: http://jsbin.com/xorov/1/edit

Upvotes: 2

Related Questions