0

I'm using Ember Data canary build: 1.0.0-beta.8+canary.267214b9 together with a rails back-end and ActiveModelSerializer. My configuration on ember side looks like this:

App.ApplicationSerializer = DS.ActiveModelSerializer.extend()

App.ApplicationAdapter = DS.ActiveModelAdapter.extend
  namespace: "api/v1"

App.Authentication = DS.Model.extend
  provider: DS.attr('string')
  user: DS.belongsTo('user')

App.User = DS.Model.extend
  username: DS.attr('string')
  email: DS.attr('string')
  authentications: DS.hasMany('authentication')

I have working hasMany and belongsTo relation for a model that isn't side loaded. The JSON for the relation look like this:

 {
   objectA: {
     property1: 'prop1',
     property2: 'prop2',
     objectB_ids: ['1','2']
   }
 }

At the moment I try to get a user model with multiple authentications to work. But there the authentications should be side loaded. It doesn't work for the following JSON:

JSON - not working

{
  authentications: [{ id:1, provider:"google" }],
  user: {
    id: '1',
    username: 'max',
    email: 'max@examle.com',
    authentication_ids:[1],
  }
}

But it does work for this:

JSON - working

{
  authentications: [{ id:1, provider:"google" }],
  user: {
    id: '1',
    username: 'max',
    email: 'max@examle.com',
    authentications:[1],
  }
}

The only useful information I found on the web is this SO question:

Serialising async hasMany relationships

Is this a bug in the DS.ActiveModelSerializer or did I miss some configuration?

EDIT 1:

In the docs of DS.ActiveModelSerializer you can find the following:

It has been designed to work out of the box with the activemodelserializers Ruby gem.

And the version with authentication_ids:[...] is the way, how the ActiveModelSerializers Ruby gem does it out of the box. So maybe it should be added?

Community
  • 1
  • 1
kunerd
  • 1,076
  • 10
  • 25

2 Answers2

1

I think you're confusing what ActiveModelSerializer does with other conventions of Ember Data. You're working second example is correct. This section describes the current expectation of JSON layout. The _ids is not present.

{
  "post": {
    "id": 1,
    "title": "Rails is omakase",
    "comments": ["1", "2"],
    "user" : "dhh"
  },

  "comments": [{
    "id": "1",
    "body": "Rails is unagi"
  }, {
    "id": "2",
    "body": "Omakase O_o"
  }]
}

The ActiveModelSerializer adapter allows you to pass underscored keys in your JSON instead of camelcased keys. For example, if your user had a camelcased name:

App.User = DS.Model.extend
  firstName: DS.attr()

Your JSON should look like this:

{
  "user": {
    "first_name": "kunerd"
  }
}
claptimes
  • 1,615
  • 15
  • 20
  • Thanks for your help, but why does it work for models, that aren't sideloaded? – kunerd May 24 '14 at 12:57
  • I'm not sure I understand your question. – claptimes May 25 '14 at 13:06
  • I mean it: the `_ids` field works on models where the associated models are loaded asynchronous, but it doesn't work for models where the associated models are side loaded in the same response. That seems a little bit inconsistent for me. Also have a look on my edit from yesterday, if you haven't already. – kunerd May 25 '14 at 17:45
0

Solved my issue and DS.ActiveModelSerializer works as expected and did accept _ids array for side loaded models.

My problem was, that I had overwritten my App.UserSerializer with that:

 App.UserSerializer = DS.RESTSerializer.extend
   # some custom logic

,but it has to be:

 App.UserSerializer = App.ApplicationSerializer.extend
   # some custom logic

Maybe someone has similar problems after changing from DS.RESTSerializer to DS.ActiveModelSerializer`

kunerd
  • 1,076
  • 10
  • 25