Last active
August 29, 2015 13:57
-
-
Save validkeys/9642588 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // this is my goal JSON output | |
| { | |
| post:{ | |
| ... | |
| comments: [1,2,3] | |
| }, | |
| comments:[{ | |
| comment:{ | |
| ... | |
| author_id: 1 | |
| } | |
| }] | |
| authors:[{ | |
| author:{ | |
| id: 1 | |
| } | |
| }] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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