Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
votes with active model serializer
class VotesGroupSerializer < ActiveModel::Serializer
attributes :group_name
has_many :votes
end
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
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