mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Document polymorphic associations in README.md
This commit is contained in:
parent
2fec5111ef
commit
09a166b76b
75
README.md
75
README.md
@ -467,9 +467,6 @@ You may also use the `:serializer` option to specify a custom serializer class a
|
|||||||
|
|
||||||
Serializers are only concerned with multiplicity, and not ownership. `belongs_to` ActiveRecord associations can be included using `has_one` in your serializer.
|
Serializers are only concerned with multiplicity, and not ownership. `belongs_to` ActiveRecord associations can be included using `has_one` in your serializer.
|
||||||
|
|
||||||
NOTE: polymorphic was removed because was only supported for has\_one
|
|
||||||
associations and is in the TODO list of the project.
|
|
||||||
|
|
||||||
## 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
|
||||||
@ -714,6 +711,78 @@ data looking for information, is extremely useful.
|
|||||||
If you are mostly working with the data in simple scenarios and manually making
|
If you are mostly working with the data in simple scenarios and manually making
|
||||||
Ajax requests, you probably just want to use the default embedded behavior.
|
Ajax requests, you probably just want to use the default embedded behavior.
|
||||||
|
|
||||||
|
|
||||||
|
## Embedding Polymorphic Associations
|
||||||
|
|
||||||
|
Because we need both the id and the type to be able to identify a polymorphic associated model, these are serialized in a slightly different format than common ones.
|
||||||
|
|
||||||
|
When embedding entire objects:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class PostSerializer < ActiveModel::Serializer
|
||||||
|
attributes :id, :title
|
||||||
|
has_many :attachments, polymorphic: true
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"post": {
|
||||||
|
"id": 1,
|
||||||
|
"title": "New post",
|
||||||
|
"attachments": [
|
||||||
|
{
|
||||||
|
"type": "image"
|
||||||
|
"image": {
|
||||||
|
"id": 3
|
||||||
|
"name": "logo"
|
||||||
|
"url": "http://images.com/logo.jpg"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "video"
|
||||||
|
"video": {
|
||||||
|
"id": 12
|
||||||
|
"uid": "XCSSMDFWW"
|
||||||
|
"source": "youtube"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
When embedding ids:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class PostSerializer < ActiveModel::Serializer
|
||||||
|
embed :ids
|
||||||
|
|
||||||
|
attributes :id, :title
|
||||||
|
has_many :attachments, polymorphic: true
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"post": {
|
||||||
|
"id": 1,
|
||||||
|
"title": "New post",
|
||||||
|
"attachment_ids": [
|
||||||
|
{
|
||||||
|
"type": "image"
|
||||||
|
"id": 12
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "video"
|
||||||
|
"id": 3
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Customizing Scope
|
## Customizing Scope
|
||||||
|
|
||||||
In a serializer, `current_user` is the current authorization scope which the controller
|
In a serializer, `current_user` is the current authorization scope which the controller
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user