Skip to content

Instantly share code, notes, and snippets.

@validkeys
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save validkeys/9642588 to your computer and use it in GitHub Desktop.

Select an option

Save validkeys/9642588 to your computer and use it in GitHub Desktop.
// this is my goal JSON output
{
post:{
...
comments: [1,2,3]
},
comments:[{
comment:{
...
author_id: 1
}
}]
authors:[{
author:{
id: 1
}
}]
}
# CONTROLLER
class PostsController < ..
def show
@post = Post.find(1)
render json: @post
end
end
# Base Serializer
class BaseSerializer < ..
embed :ids, include: true
end
# Post Serializer
class PostSerializer < BaseSerializer
attributes :id, ...
has_many :comments
end
# Comment Serializer
class CommentSerializer < BaseSerializer
attributes :id, :text ...
# by including this relationship I am getting
# no implicit conversion of String into Integer
# if I remove it, the error goes away
has_one :author
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment