Last active
August 29, 2015 14:24
-
-
Save Tmw/fa18019595a36e3f190a to your computer and use it in GitHub Desktop.
Combining multiple Active_Model_Serializers in one output
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 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