Has_many configuration for Ember-Data and Active Model serializers with built-in identifiers and side-loading

I know that Ember-Data should be compatible with Serializers Active Model by design, but they don't seem to be able to serialize has_many relationships with inline identifiers.

For example, serializer

 class PostSerializer < ActiveModel::Serializer embed :ids has_many :comments end 

creates json

 { "post": { "comment_ids": [...] } } 

But the default configuration in Ember Data,

 App.Post = DS.Model.extend({ DS.hasMany('App.Comment'), }); App.Comment = DS.Model.extend(); 

expects that the association of comments will be serialized as comments: [...] without the _ids suffix (see the relationship section of the Ember.js REST adapter section ).

I tried the following:

 class PostSerializer < ActiveModel::Serializer attribute :comments def comments object.comment_ids end end 

This works, but adding embed :ids, :include => true to enable lateral loading now does nothing, because AMS does not know that this is an association.

Edit: I am using active_model_serializers (0.6.0) gem and version of Ember-Data 11

+6
source share
4 answers

For ember-data 1.0.0-beta.3, I used this:

 App.ApplicationSerializer = DS.ActiveModelSerializer.extend({}); 

As explained here: Transition Guide

It works well!

+7
source

You can try to configure the correct mapping in the adapter on the client side.

 DS.RESTAdapter.map('App.Post', { comments: { keyName: 'comment_ids' } }); 
+3
source

I am using active_model_serializers 0.6.0 and ember_data 11. I do not see the behavior you are reporting.

My serializer:

 class CentreSerializer < ActiveModel::Serializer embed :ids attributes :id, :name has_many :rooms end 

Localhost output: 3000 / centers / 1.json

 { centre: { id: 1, name: "Centre0", rooms: [ 1, 2, 3, 4, 5 ] } } 

In my case, the rails application creates a properly formed json before it even gets into ember. You do not need to resort to display on the client side.

+2
source

This commit seems to be responsible for the situation. When AMS was updated to serialize has_one association_id as association_id (matching AMS with ember data), it was also changed to serialize belongs_to association_ids as association_ids .

0
source

All Articles