Created
April 11, 2015 23:14
-
-
Save skunkworker/599a398def2a2d6e3ee8 to your computer and use it in GitHub Desktop.
votes with active model serializer
This file contains 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
class VotesGroupSerializer < ActiveModel::Serializer | |
attributes :group_name | |
has_many :votes | |
end |
This file contains 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
class VoteSerializer < ActiveModel::Serializer | |
attributes :created_at, :user, :hashtag | |
def user | |
{id:object.user.id, username:object.user.username} | |
end | |
def hashtag | |
{id:object.hashtag.id, name:object.hashtag.name} | |
end | |
end |
This file contains 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
render json: { | |
near_me: { | |
count: @near_me.count, | |
votes_groups: ActiveModel::ArraySerializer.new(@votes_groups, each_serializer: VotesGroupsSerializer) | |
}, | |
votes: ActiveModel::ArraySerializer.new(@votes, each_serializer: VoteSerializer) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment