mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
#133 adding documentation for :serializer and :polymorphic options on associations
This commit is contained in:
parent
bea38dd73a
commit
257adfc80e
31
README.md
31
README.md
@ -328,6 +328,37 @@ class PostSerializer < ActiveModel::Serializer
|
|||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Use the `:serializer` option to specify a custom serializer class:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class PostSerializer < ActiveModel::Serializer
|
||||||
|
attributes :id, :title, :body
|
||||||
|
has_one :author
|
||||||
|
has_many :comments, :serializer => CommentShortSerializer
|
||||||
|
|
||||||
|
def include_associations!
|
||||||
|
include! :author if scope.admin?
|
||||||
|
include! :comments unless object.comments_disabled?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
Use the `:polymorphic` option to specify an association that is polymorphic (STI):
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class PostSerializer < ActiveModel::Serializer
|
||||||
|
attributes :id, :title, :body
|
||||||
|
has_one :author
|
||||||
|
has_many :comments
|
||||||
|
has_one :reviewer, :polymorphic => true
|
||||||
|
|
||||||
|
def include_associations!
|
||||||
|
include! :author if scope.admin?
|
||||||
|
include! :comments unless object.comments_disabled?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
## Embedding Associations
|
## Embedding Associations
|
||||||
|
|
||||||
By default, associations will be embedded inside the serialized object. So if
|
By default, associations will be embedded inside the serialized object. So if
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user