0

I'm working with Ember with Rails Backend

Ember.VERSION : 1.0.0-rc.3 ember.js:349
Handlebars.VERSION : 1.0.0-rc.3 ember.js:349
jQuery.VERSION : 1.9.1 

I have three models

App.SocialNetwork = DS.Model.extend({
  name: DS.attr('string'),
  actors: DS.hasMany('App.Actor'),
  relations: DS.hasMany('App.Relation'),
});

App.Actor = DS.Model.extend({
  name: DS.attr('string'),
  x: DS.attr('number'),
  y: DS.attr('number'),
  social_network: DS.belongsTo('App.SocialNetwork'),
  relations: DS.hasMany('App.Relation'),
  isSelected: function () {
   return false;
  }.property(),
});

App.Relation = DS.Model.extend({
  name: DS.attr('string'),
  actors: DS.hasMany('App.Actor'),
  social_network: DS.belongsTo('App.SocialNetwork'),
});

And Inside my RelationsController I want to create a new instance of Relation

I tried to do it like this

App.RelationsController = Ember.ArrayController.extend({
  currentRelation: null,
  add: function () {
    // get the selected actors
    var actors = this.get('socialNetwork.actors').toArray().filter(function (element) {
      return element.get("isSelected") == true;
    });
    // create the new relation with those actors
    var newRelation = App.Relation.createRecord({
      actors: actors,
      name: "New Relation",
    });
    this.set('currentRelation', newRelation);
    this.get('content').pushObject(newRelation);
    this.get('store').commit();
  },
});

But the relation is not being stored, and I debug the new record created and it doesn't have any actors or social network associations.

What I'm doing wrong or not doing here?

Thanks in advance for your help

PS: by the way, the socialNetwork and actors load correctly

fespinozacast
  • 2,384
  • 3
  • 21
  • 36
  • Have you seen [this](http://stackoverflow.com/a/14324532/54364)? – MilkyWayJoe May 03 '13 at 20:17
  • yes, now I can persist my data correctly, but I have a new problem, loading relation records from rails doesn't load its actors. I asume that expects to embed actors in the relation serializer but if I do that, then the program crashes – fespinozacast May 07 '13 at 00:53
  • The solution that finally worked for me I found it on http://stackoverflow.com/questions/15683273/save-foreign-key-to-other-model-with-hasmany-relation/15709988#15709988 – fespinozacast May 07 '13 at 15:56

1 Answers1

1

It looks like you forgot belongsTo side of the relationship. Add

relation: DS.belongsTo('App.Relation')

to your App.Actor model.

mehulkar
  • 4,624
  • 4
  • 30
  • 50