Reflexive relationships


#1

Has anyone gotten Ember to work with Reflexive relationships? I’m using ember-data-1.0.0.beta.7 with ActiveModelAdapter and Rails. My model looks like:

App.Foobar = DS.model.extend({
  name: DS.attr('string'),
  ancestor: DS.belongsTo('foobar'),
  descendants: DS.hasMany('foobar')
});

and the returned JSON looks like:

{
  "foobar": {"id": 2, "name": "lorem"},
  "descendants": [
    {"id": 3, "name": "John"},
    {"id": 4, "name": "Smith"}
  ],
  "ancestor": {"id": 6, "name": "ipsum"}
}

I believe the JSON format is what is described in the documentation, but I wouldn’t be surprised if there’s something amiss. I also tried turning async to false and I still got the same error.

This is the error I got:

"Error while loading route: Ember.Error@http://localhost:3000/assets/ember.js?body=1:837
Store<.modelFor@http://localhost:3000/assets/ember-data.js?body=1:9806
RESTSerializer<.extractSingle@http://localhost:3000/assets/ember-data.js?body=1:3019
superWrapper@http://localhost:3000/assets/ember.js?body=1:1231
JSONSerializer<.extractFind@http://localhost:3000/assets/ember-data.js?body=1:2481
JSONSerializer<.extract@http://localhost:3000/assets/ember-data.js?body=1:2366
_find/<@http://localhost:3000/assets/ember-data.js?body=1:10338
invokeCallback@http://localhost:3000/assets/ember.js?body=1:9428
publish@http://localhost:3000/assets/ember.js?body=1:9098
publishFulfillment@http://localhost:3000/assets/ember.js?body=1:9518
DeferredActionQueues.prototype.flush@http://localhost:3000/assets/ember.js?body=1:5651
Backburner.prototype.end@http://localhost:3000/assets/ember.js?body=1:5742
Backburner.prototype.run@http://localhost:3000/assets/ember.js?body=1:5781
Ember.run@http://localhost:3000/assets/ember.js?body=1:6182
RESTAdapter<.ajax/< /hash.success@http://localhost:3000/assets/ember-data.js?body=1:1524
jQuery.Callbacks/fire@http://localhost:3000/assets/jquery.js?body=1:3049
jQuery.Callbacks/self.fireWith@http://localhost:3000/assets/jquery.js?body=1:3161
done@http://localhost:3000/assets/jquery.js?body=1:8236
.send/callback@http://localhost:3000/assets/jquery.js?body=1:8779
"

And in the Ember Inspector it tells me a promise was rejected because the model wasn’t found.

Any help would be greatly appreciated.


#2

I am able to get it working with the FixtureAdapter, but still no luck using the ActiveModelAdapter. A couple of corrections to the code above.

  1. In the Foobar model, ancestor needs to have many foobar as well
    ancestor: DS.hasMany(‘foobar’)
  2. The returned JSON needs to embed the descendants and ancestor IDs within “foobar”:
{
  "foobar": {"id": 2, "name": "lorem", "descendants": [3,4], "ancestors": [6]},
  "descendants": [
    {"id": 3, "name": "John"},
    {"id": 4, "name": "Smith"}
  ],
  "ancestor": {"id": 6, "name": "ipsum"}
}

Again, any help with this is greatly appreciated. Mike


#3

Hey I’m trying to solve something similar. I’m using the ancestry gem for parent/children relationships, is that what you mean by reflexive?

The error I get is only on the show pages for an entry with a parent. I get Error: No model was found for 'ancestor'.

More details here: http://stackoverflow.com/questions/24474369/error-no-model-was-found-for-ancestor-in-ember


#4

Just found these entries on SO and here:


#5

Is there any example for Ember reflexive relation ?


#6

Ya, check out this repo I made about this exact thing.

It’s not CLI, but it was how I got it working in my app.