2
votes

I have pinpointed some behavior I'd like to see if anyone can explain.

I made a jsfiddle here http://jsfiddle.net/iceking1624/PpWec/17/. To test behavior, open web console and simply type something in the textfield next to 'Add Question' button and then hit the button('Add Question').

Here are the DS.Model schemas:

App.Session = DS.Model.extend({
   "questions" : DS.hasMany('App.Question') ,
   "firstQuestion" : DS.belongsTo('App.Question')
});
App.Question = DS.Model.extend({
   "session" : DS.belongsTo('App.Session'),
   "choices" : DS.hasMany('App.Choice'),
   "text"    : DS.attr('string')
});
App.Choice = DS.Model.extend({
   "session"  : DS.belongsTo('App.Session'),
   "question" : DS.belongsTo('App.Question'),
   "text"     : DS.attr('string')
});

When I do the following inside SessionController then call the addQuestion function:

App.SessionController = Ember.ObjectController.extend({
   addQuestion: function() {
      var sessionModel = this.get('model');

      var question = App.Question.createRecord({
         "session" : sessionModel
      });

      this.get('questions').pushObject(question)
   }
});

I get the following 'error' but it functions as expected nonetheless:

Error: assertion failed: You defined the 'session' relationship on App.Question, but multiple possible inverse relationships of type App.Question were found on App.Session.

Now I figured out it's because inside App.Session I have two fields that reference App.Question:

   "questions" : DS.hasMany('App.Question') ,
   "firstQuestion" : DS.belongsTo('App.Question')

but I'm wondering why Ember cares that I have multiple App.Question relationships?? And especially why it would throw an error, yet work perfectly. Is this a bug perhaps?

Is there a way I can make Ember happy and not throw an error??? I really need that firstQuestion field in my App.Session model.

Open the console and look at my jsfiddle. It's a perfect example of my issue. Then try commenting out firstQuestion in the App.Session = DS.Model and watch how the error magically disappears

1

1 Answers

8
votes

So I found a solution. I hope this helps somebody in the future!

Apparently it was improper to define multiple relationships to the same model as I did, without being a bit more descriptive...

Below I have both questions and firstQuestion pointing to App.Question and Ember did not like that I was so vague!

App.Session = DS.Model.extend({
   "questions" : DS.hasMany('App.Question') ,
   "firstQuestion" : DS.belongsTo('App.Question')
});

To reduce chances for miscommunication (and rid an annoying error), the inverse relation can be explicitly defined as below:

App.Session = DS.Model.extend({
    "questions" : DS.hasMany('App.Question', { inverse: 'session' }),
    "firstQuestion" : DS.belongsTo('App.Question')
});
App.Question = DS.Model.extend({
    "session" : DS.belongsTo('App.Session', { inverse: 'questions' }),
    "choices" : DS.hasMany('App.Choice'),
    "text"    : DS.attr('string')
});

this got rid of the error. If anyone else has more insight or even criticism I'd be interested to hear.