mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Explicitly set serializer for associations
Document specifying serializer for assocaition
This commit is contained in:
23
test/fixtures/poro.rb
vendored
23
test/fixtures/poro.rb
vendored
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user