Ember Rails & JSON Serialization
Written on 2/22/14, using Ember Rails 0.14.1, Ember Data Source >= 1.0.0.beta.5, and Active Model Serializers 0.9.0.pre.
I've set up a couple of Ember projects (mostly anecdotal things), as well as had the opportunity to work on a built out Ember-Rails app created by another developer. JSON serialization has been a consistent gotcha for me and others, so I wanted to write down what I've learned. Thanks Rose Auravide for inspiring me to get to the bottom of this.
Active Model Serializers
As it stands right now Active Model Serializers work really seamlessly with Ember Data. It supports sideloading, as well as outputs the appropriate child root keys. Make sure you restart your server after you bundle install, or Rail's implementation of
to_json won't search for your AMS.
Whether you used scaffolding, or wrote your own controllers, when you let Rails implicitly renders your JSON, it won't use AMS. Be sure to explictly state
render json: @collection, like the following
Active Model Serializer claims to work with
respond_with, however in my experience it does not use the serializers. There is an outstanding Github issue for this problem, hopefully I can fix it soon, or someone else :) The fallback way to do this being:
What Ember Data Wants
Now that you're using Active Model Serializers your JSON is going to be pretty as can be. To ensure sideloading happens just use
embeds :id, include: true and the appropriate AMS Association.
Which gives us the perfect: