rails-api/active_model_serializers — pull request #742 by carsomyr (2014-11-29 00:18:35+00:00)
Title: Serialize polymorphic, embed-in-root relationships by class name instead of association name

Since entities are embedded at the root level, polymorphic associations should use the class name under polymorphism instead of the association name. `has_one` and `has_many` relationships have been updated to reflect this, and `DefaultSerializer` now represents polymorphic types just as `Serializer` would.

This change brings active_model_serializers much closer to supporting Ember Data sideloading with polymorphism and the [`ActiveModelAdapter`](http://emberjs.com/api/data/classes/DS.ActiveModelAdapter.html). On the Rails side, you need an initializer of the form:

ActiveModel::Serializer.setup do |config|
config.embed = :ids
config.embed_in_root = true
end

... and a `key` option in associations to ensure that `key_name` gets serialized as `key_name` instead of `key_name_id`:

has_one :content, polymorphic: true, key: "content"

Admittedly, I made this PR to bring an end to monkey patching of our active_model_serializer / Ember Data transport layer, but I do believe that people would want this type of embed-in-root behavior regardless of what consumes the JSON. Also, getting an API server and JavaScript MVC framework to play well together would be a nice showpiece for complex serialization done right, i.e., with polymorphism *and* sideloading.