rails-api/active_model_serializers — pull request #367 by jonah-carbonfive (2013-07-30 18:40:54+00:00)
Title: has_many supports polymorphic associations

See issue #366.

Previously `has_many` associations ignored the `polymorphic: true` option.

Now polymorphic has_many associations behave similarly to has_one associations. For included associations objects are grouped under root keys derived from their types rather than the association name.

For compatibility if a root key is specified on a embedded included polymorphic has_many association then all objects in that association will grouped under the specified root key instead of a key for each object type.

# polymorphic has_many

```
has_many :edibles, polymorphic: true
```

```
{
edibles: [
{
type: :orange,
orange: {
...
}
}
{
type: :banana,
banana: {
...
}
}
]
}
```

# embedded polymorphic has_many

```
has_many :edibles, embed: :ids, polymorphic: true
```

```
{
edibles: [
{
type: :orange,
id: 1
}
{
type: :banana,
id: 1
}
]
}
```

# included embedded polymorphic has_many

```
has_many :edibles, embed: :ids, include: true, polymorphic: true
```

```
{
meals: [{
edibles: [
{
type: :orange,
id: 1
}
{
type: :banana,
id: 1
}
]
}],
oranges: [{
id: 1,
...
}],
bananas: [{
id: 1,
...
}]
}
```