mathieuprog
Hello ![]()
I published a library that brings support for polymorphic/dynamic embeds in Ecto.
Ecto’s embeds_one macro requires a specific schema module to be specified. This library removes this restriction by dynamically determining which schema to use, based on data to be stored (from a form or API) and retrieved (from the data source).
Example use case:
Say you have a Reminder schema. A reminder has a set date and text, but also specific fields whether it is an email reminder or sms reminder. Instead of adding and mixing all the email- and sms-related fields together in the Reminder schema, where one set of fields or the other are null values, you can add a polymorphic embed, only containing the relevant sms or email fields. The polymorphic embed also supports its own changeset validations.
See example code and usage in Readme file ![]()
https://github.com/mathieuprog/polymorphic_embed
Inspired by ecto_poly library.
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 16 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
munksgaard
This seems to work the first time I select something. But if I then change my selection, it doesn’t actually change anything. Even in my
handle_eventI’m not seeing the updated selection.sodapopcan
Big frameworks like Rails and Django calls the relational non-embedded versions “polymorphic associations” as does Ecto so that is likely what most people would be searching for.
murrgelb
I really love this and often faced the problem. The reason I haven’t found this before is the naming. Isn’t that more of a (algebraic) sum-type than polymorphism?
djcoin
You just need to manipulate the type field in the polymorphic field (by default
__type__).So, given a “type” field (of type string) you could try something along this for example:
linusdm
At work we got stuck using the library to allow changing the type using a form. Is polymorphic_embed intended for this usecase? Or is the type meant to be chose once at creation time, and then stay fixed throughout its lifetime?
Edit: sorry, did not intend to make this a reply… it was meant to be a general question.
KP123
The module,
PolymorphicEmbed.HTML.Formappears to be entirely outdated for forms in Phoenix 1.7.2 and above. I neededPolymorphicEmbed.HTML.Form.get_polymorphic_type/3to render the UI properly so I rewrote it:mathieuprog
A tricky bug has been fixed where atoms were not persisted in the compilation files. The atoms specifying the types were converted into strings in the Ecto Type’s
init/1function at compile-time, and so as we only worked with the strings afterwards, the atoms were not persisted:https://github.com/mathieuprog/polymorphic_embed/issues/59
Fixed in 3.0.3 thanks to the support of @LostKobrakai on Slack and the contribution of Alessio !
mathieuprog
Version 3.0.0 has been released!
We now encourage the use of
polymorphic_embeds_one/2andpolymorphic_embeds_many/2macros to define your polymorphic embeds.If you’re curious as to why: a field containing a polymorphic list of embeds defaulted to
nilbefore version 3. To followembeds_many/3we should default to[]. This can only be done by forcing the:defaultoption of thefield/3to[](ie we can’t configure this in the Type). I added the new macros in order to set these kind of defaults setting up the field for the polymorphic embeds.Thanks to Alexandre @Matsa59 for his contribution which led to this update!
mathieuprog
Support for LiveView forms has been added thanks to the quality contribution of Mathias!
Support for primary keys has been added
The latter may introduce breaking changes, therefore Polymoprhic Embed version has been bumped to 2.0.0
Migration from 1.x to 2.x is easy:
embedded_schemacontains the setting@primary_key falsebelaustegui
Just wanted to jump in and say thanks for this awesome library @mathieuprog
We have been using it for the last few months at my day job and it has been an absolute pleasure. Works well and reliably and the documentation is top-notch.