# Polymorphic\_embed - polymorphic Embeds in Ecto

**URL:** <https://forum.elixirforum.com/t/polymorphic-embed-polymorphic-embeds-in-ecto/31951>\
**Category:** Announcing\
**Tags:** ecto, library, schema, polymorphism, embedded\_schema, polymorphic\_embed\
**Created:** [May 30, 2020, 5:15pm UTC](https://forum.elixirforum.com/t/polymorphic-embed-polymorphic-embeds-in-ecto/31951 "2020-05-30T17:15:35Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![mathieuprog](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mathieuprog/32/16860_2.png) [@mathieuprog](https://forum.elixirforum.com/u/mathieuprog)\
**Post date:** [June 2, 2020, 6:43am UTC](https://forum.elixirforum.com/t/polymorphic-embed-polymorphic-embeds-in-ecto/31951/3 "2020-06-02T06:43:42Z")

</div>

I imagine this is a pretty common need, and that many codebases would either have a schema with sets of null values, or end up having their own custom Ecto type implementations.

This library indeed provides a custom Ecto Type that determines the right embed schema to use, based on params to cast. Nice thing is, you can tell `PolymorphicEmbed` that the presence of specific params identifies a certain schema – no need for a “type” field in the params:)

In addition to the advantages that a library offers (get rid of boilerplate code in the app, reusable code across projects, bugfixes, …), it comes with some nice features:

- As mentioned, detect which types to use for the data being `cast`-ed, based on fields present in the data (no need for a _type_ field in the data)
- Run changeset validations when a `changeset/2` function is present (when absent, the library will introspect the fields to cast)
- Support for nested polymorphic embeds
- Support for nested `embeds_one`/`embeds_many` embeds
- Display form inputs for polymorphic embeds in Phoenix templates

Added those features in the Readme file.

---

_[View the full topic](https://forum.elixirforum.com/t/polymorphic-embed-polymorphic-embeds-in-ecto/31951)._
