Ember.JS ActiveModelAdapter and Active Model Default Serializers

I have a simple active model serializer:

class ActivitySerializer < ActiveModel::Serializer attributes :id, :title, :description, :time has_one :category has_one :user end 

I also have category and user serializers, and they work as expected. I get this payload:

 {"activities":[{"id":1,"title":"Test Activity","description":null,"time":"2014-03-01T06:05:41.027Z","category":{"id":1,"title":"Sports"},"user":{"id":1,"name":"ember"}}]} 

However, they do not load in ember.

 App.Activity = DS.Model.extend title: DS.attr('string') description: DS.attr('string') time: DS.attr('date') category: DS.belongsTo('category') user: DS.belongsTo('user') App.Category = DS.Model.extend title: DS.attr('string') activities: DS.hasMany('activity') App.User = DS.Model.extend name: DS.attr('string') activities: DS.hasMany('activity') 

When I check the ember inspector, the data is not loading. What format does ActiveModelSerializer expect? It loads activity, but not category or user attributes.

+6
source share
2 answers

The trick took me a bit to find online; my model was to include embed :ids .

 class ActivitySerializer < ActiveModel::Serializer embed :ids, include: true attributes :id, :title, :description, :time has_one :category has_one :user end 

Alternatively , you can do something according to the line, but no promises I have never tested this code.

 App.ActivitySerializer = DS.ActiveModelSerializer.extend DS.EmbeddedRecordsMixin, attrs: user: {embedded: 'always'} category: {embedded: 'always'} App.ApplicationAdapter = DS.ActiveModelAdapter.extend defaultSerializer: 'DS/app' 
+7
source

The deployment identifier Ryan mentioned is working, but you may have problems with kinks, for example, organized_by becomes organized_bies, and then you must configure both sides to fix it. JS code needed to fix this problem:

 DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, { attrs: { category: {embedded: 'always'} } }); 
+3
source

All Articles