Skip to content

Instantly share code, notes, and snippets.

@skunkworker
Created April 11, 2015 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skunkworker/599a398def2a2d6e3ee8 to your computer and use it in GitHub Desktop.
Save skunkworker/599a398def2a2d6e3ee8 to your computer and use it in GitHub Desktop.
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