Skip to content

Commit 09a166b

Browse files
committed
Document polymorphic associations in README.md
1 parent 2fec511 commit 09a166b

File tree

1 file changed

+79
-10
lines changed

1 file changed

+79
-10
lines changed

README.md

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
[![Build Status](https://api.travis-ci.org/rails-api/active_model_serializers.png?branch=0-9-stable)](https://travis-ci.org/rails-api/active_model_serializers)
2-
[![Code Climate](https://codeclimate.com/github/rails-api/active_model_serializers.png)](https://codeclimate.com/github/rails-api/active_model_serializers)
1+
[![Build Status](https://api.travis-ci.org/rails-api/active_model_serializers.png?branch=0-9-stable)](https://travis-ci.org/rails-api/active_model_serializers)
2+
[![Code Climate](https://codeclimate.com/github/rails-api/active_model_serializers.png)](https://codeclimate.com/github/rails-api/active_model_serializers)
33

44
# ActiveModel::Serializers
55

66
## Purpose
77

8-
`ActiveModel::Serializers` encapsulates the JSON serialization of objects.
9-
Objects that respond to read\_attribute\_for\_serialization
8+
`ActiveModel::Serializers` encapsulates the JSON serialization of objects.
9+
Objects that respond to read\_attribute\_for\_serialization
1010
(including `ActiveModel` and `ActiveRecord` objects) are supported.
1111

1212
Serializers know about both a model and the `current_user`, so you can
@@ -229,7 +229,7 @@ ActiveModel::Serializer.setup do |config|
229229
config.key_format = :lower_camel
230230
end
231231

232-
class BlogLowerCamelSerializer < ActiveModel::Serializer
232+
class BlogLowerCamelSerializer < ActiveModel::Serializer
233233
format_keys :lower_camel
234234
end
235235

@@ -467,9 +467,6 @@ You may also use the `:serializer` option to specify a custom serializer class a
467467

468468
Serializers are only concerned with multiplicity, and not ownership. `belongs_to` ActiveRecord associations can be included using `has_one` in your serializer.
469469

470-
NOTE: polymorphic was removed because was only supported for has\_one
471-
associations and is in the TODO list of the project.
472-
473470
## Embedding Associations
474471

475472
By default, associations will be embedded inside the serialized object. So if
@@ -646,8 +643,8 @@ The above would yield the following JSON document:
646643
}
647644
```
648645

649-
When side-loading data, your serializer cannot have the `{ root: false }` option,
650-
as this would lead to invalid JSON. If you do not have a root key, the `include`
646+
When side-loading data, your serializer cannot have the `{ root: false }` option,
647+
as this would lead to invalid JSON. If you do not have a root key, the `include`
651648
instruction will be ignored
652649

653650
You can also specify a different root for the embedded objects than the key
@@ -714,6 +711,78 @@ data looking for information, is extremely useful.
714711
If you are mostly working with the data in simple scenarios and manually making
715712
Ajax requests, you probably just want to use the default embedded behavior.
716713

714+
715+
## Embedding Polymorphic Associations
716+
717+
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.
718+
719+
When embedding entire objects:
720+
721+
```ruby
722+
class PostSerializer < ActiveModel::Serializer
723+
attributes :id, :title
724+
has_many :attachments, polymorphic: true
725+
end
726+
```
727+
728+
```json
729+
{
730+
"post": {
731+
"id": 1,
732+
"title": "New post",
733+
"attachments": [
734+
{
735+
"type": "image"
736+
"image": {
737+
"id": 3
738+
"name": "logo"
739+
"url": "http://images.com/logo.jpg"
740+
}
741+
},
742+
{
743+
"type": "video"
744+
"video": {
745+
"id": 12
746+
"uid": "XCSSMDFWW"
747+
"source": "youtube"
748+
}
749+
}
750+
]
751+
}
752+
}
753+
```
754+
755+
When embedding ids:
756+
757+
```ruby
758+
class PostSerializer < ActiveModel::Serializer
759+
embed :ids
760+
761+
attributes :id, :title
762+
has_many :attachments, polymorphic: true
763+
end
764+
```
765+
766+
```json
767+
{
768+
"post": {
769+
"id": 1,
770+
"title": "New post",
771+
"attachment_ids": [
772+
{
773+
"type": "image"
774+
"id": 12
775+
},
776+
{
777+
"type": "video"
778+
"id": 3
779+
}
780+
]
781+
}
782+
}
783+
```
784+
785+
717786
## Customizing Scope
718787

719788
In a serializer, `current_user` is the current authorization scope which the controller

0 commit comments

Comments
 (0)