Skip to content

Instantly share code, notes, and snippets.

@joshskeen
Last active January 12, 2016 21:38
Show Gist options
  • Save joshskeen/3fed3921efb8e974b913 to your computer and use it in GitHub Desktop.
Save joshskeen/3fed3921efb8e974b913 to your computer and use it in GitHub Desktop.
how to use a plain old ruby object PORO and an active_model_serializers Serializer (0.10.x)
ruby object:
class SearchResult
extend ActiveModel::Naming
include ActiveModel::Serialization
attr_accessor :stories, :users, :friends
def id
hash
end
def initialize(search:)
@stories = search.stories
@users = search.users
@friends = search.friends
end
def attributes
{ stories: stories,
users: users,
friends: friends
}
end
end
Serializer:
class SearchResultSerializer < ActiveModel::Serializer
attributes :stories, :users, :friends
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment