Explicitly set serializer for associations

Document specifying serializer for assocaition
This commit is contained in:
Gary Gordon
2014-10-24 08:23:07 -04:00
parent 08fbba9087
commit 9f9715801a
6 changed files with 119 additions and 13 deletions

23
test/fixtures/poro.rb vendored
View File

@@ -100,3 +100,26 @@ AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do
attribute :id
attribute :name, key: :title
end
CommentPreviewSerializer = Class.new(ActiveModel::Serializer) do
attributes :id
belongs_to :post
end
AuthorPreviewSerializer = Class.new(ActiveModel::Serializer) do
attributes :id
has_many :posts
end
PostPreviewSerializer = Class.new(ActiveModel::Serializer) do
def self.root_name
'posts'
end
attributes :title, :body, :id
has_many :comments, serializer: CommentPreviewSerializer
belongs_to :author, serializer: AuthorPreviewSerializer
end