josevalim/active_model_serializers — pull request #43 by ctide (2012-02-23 03:40:04+00:00)
Title: Add a :root option when defining associations

This pull request allows you to define associations like this:

class PostSerializer < ApplicationSerializer
embed :ids, :include => true

attributes :id, :title, :body
has_many :comments, :key => :comment_ids, :root => :comment_objects
end

Which will allow you to define different keys for the embedded field vs. what's added to the root level JSON, like this:

{
"post": {
"id": 1,
"title": "New post",
"body": "A body!",
"comment_ids": [ 1 ]
},
"comment_objects": [
{ "id": 1, "body": "what a dumb post" }
]
}

This is partially to work around some bugs with RestKit (iOS framework, which would explode if it got JSON that had attributes that mirrored associations it was attempting to generate) but it also feels like it makes my JSON cleaner.