DS.hasMany sideloaded relationships in server response are ignored


#1

Hey guys,

I have an issue getting any sideloaded hasMany relationships to work in the ember-data 1.0.0-beta builds. I’ve gone over the docs and I can’t see what I’m doing wrong, but if I call

model.get('relationship.length')

The result is always 0, and there are no relationships. This is my test case.

I have a json response like this:

{
  "categories": [{
    "id": "b01a0378-49e9-4dd2-aa35-ff2cab8fb3a4",
    "name": "my category"
  }],
  "shop": {
    "id": "0eda4921-e2f6-4a8e-b802-e01df49bc958",
    "category_ids": [
      "b01a0378-49e9-4dd2-aa35-ff2cab8fb3a4"
    ],
  }
}

I’ve set up appropriate inflection:

Ember.Inflector.inflector.rules.irregular['category'] = 'categories'

And I have models like this:

App.Shop = DS.Model.extend
  categories: DS.hasMany 'category'

App.Category = DS.Model.extend
  name: DS.attr 'string'

My application route is as follows:

App.ApplicationRoute = Ember.Route.extend
  model: ->
    @get('store').find('shop', '0eda4921-e2f6-4a8e-b802-e01df49bc958')
  setupController: (controller, model) ->
    @_super(controller, model)
    window.model = model

In the js console, model.get(‘categories’) is always an empty array, and model.get(‘categories.type’) correctly shows “App.Category”.

Things get really weird if I call:

store.find('category', 'b01a0378-49e9-4dd2-aa35-ff2cab8fb3a4')

This returns the category, without hitting the server. It uses the stores cache, so the object is clearly getting built on the initial call.

Any idea why the relationship is never getting attached to the parent object?


#2

OK, So I solved this myself, but there is something clearly missing any documentation:

I had:

App.ApplicationAdapter = DS.RESTAdapter

Apparently RESTAdapter no longer supports snake cased json apis. If you’re using something like active model serializers, there is now:

App.ApplicationAdapter = DS.ActiveModelAdapter

Making that change made relationships work again.

The relevant source can be found here: https://github.com/emberjs/data/blob/v1.0.0-beta.3/packages/activemodel-adapter/lib/system/active_model_adapter.js

And the new docs for RESTAdapter can be found in it’s source here: https://github.com/emberjs/data/blob/v1.0.0-beta.3/packages/ember-data/lib/adapters/rest_adapter.js