Exception on side-loading a belongsTo relation #467

treet opened this Issue Nov 9, 2012 · 4 comments


None yet
5 participants

treet commented Nov 9, 2012

Edit: I rewrote this issue as I think I found the root cause of my problem.

The side loading example in the README (the author profile one) does not seem to work as intended.
See http://jsfiddle.net/DkzdF/ for an example.
When running it crashes with "Uncaught Error: assertion failed: Your server returned a hash with the key profiles but you have no mappings".

That's strange, isn't this how side-loading is supposed to work? Changing the name of the json key from 'profiles' to 'profile solves the problem but that doesn't make any sense.

I think I've narrowed it down to this piece of code: https://github.com/emberjs/data/blob/master/packages/ember-data/lib/adapters/rest_adapter.js#L310
It appears that the rest adapter thinks the json key 'profiles' refers to a non-existing DS.hasMany('App.Profile') relation in App.Author when it actually refers to the DS.belongsTo('profile') relation.
Shouldn't the sideLoad() method be iterating the model's relations and crosschecking the json for a compatible key instead of vice-verse?

In any case, the example of side-loading in the README does not seem to work anymore, is this indended? If so how is side-loading supposed to work?

Shouldn't the keys "posts" and "tags" be wrapped within the root key "user"?


elliterate commented Nov 18, 2012

The JSON is fine. You have to tell the adapter how your root JSON keys map to your models:

  mappings: {
    profiles: 'App.Profile'

It knows that "authors" maps to App.Author, because that's the model you passed into findAll, but it makes no assumptions about the other JSON keys.

That said, if you add the mapping to your fiddle, you'll run into another error because you're bypassing the store and triggering the adapter's didFindAll function by hand. Here's a fork of your fiddle that shows it working via store.findAll instead: http://jsfiddle.net/wspA5/1/

treet commented Nov 18, 2012

Awesome, thanks a lot, exactly what I was looking for. I just wished it had been documented somewhere :/


bobbus commented Jan 15, 2013

I think this issue is not relevant anymore as we should now use RESTAdapter.configure to set mapping

igorT closed this Apr 15, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment