Skip to content

Instantly share code, notes, and snippets.

@Tmw
Last active August 29, 2015 14:24
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 Tmw/fa18019595a36e3f190a to your computer and use it in GitHub Desktop.
Save Tmw/fa18019595a36e3f190a to your computer and use it in GitHub Desktop.
Combining multiple Active_Model_Serializers in one output
class PersonSerializer < ActiveModel::Serializer
attributes :first_name, :last_name
def attributes
super.merge(AddressSerializer.new(object.address).serializable_hash)
end
end
# Given a model Person with an association has_one Address
# The example above will serialize to:
# {
# "first_name" : "tiemen",
# "last_name" : "Waterreus",
# "streetname" : "Some Street",
# "zipcode" : "12345",
# "etc" : "etc"
# }
# where the streetname, zipcode, etc fields are from the AddressSerializer are combined with the results from the PersonSerializer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment