danschultzer
Pow Core Team
Recently I had to ensure semi-arbitrary data for an embedded schema could be validated and easily mapped in Phoenix forms. I didn’t need to store this data in the database. After tinkering with it for a bit polymorphic embeds was the solution.
Trending in Blog Posts
Up until then, I found it very hard to communicate to LLMs that I specifically do not want to handle X case because maybe it has never ha...
New
An automated trigger fired every two minutes for 8 hours. 243 agent runs. 31M tokens. $63 wasted. The problem: nobody noticed until morni...
New
The words are insufficient to describe what transpired. What we tried to do met what the people wanted it to be. It became beautiful.
Th...
New
TL;DR: Give https://cure-lang.org a try—perhaps you will like it.
New
Hi all,
I’ve built a small demo app to understand what an “AI agent” actually is under the hood, and to show it with Elixir’s own tools ...
New
A backtest worker and a live trading node had to share one broker rate limit and one set of OAuth tokens, and both only work if there’s e...
New
Secure boot and a verified root filesystem on an STM32MP157F-DK2 - with Nerves of course
Four links of an authenticated boot chain on an ...
New
Other Trending Topics
Boloñesa - A self-hosted media center built with Phoenix LiveView
Hey everyone!
With building software becoming more accessible than ev...
New
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
tangui
Hi Dan,
Can’t thank you enough for this great write-up!
It became very useful as I was doing some funny things with [
polymorphic_embed]( polymorphic_embed | Hex ), such as:field.form.data.<fieldname>withfield.valueIn both case, this was impossible with
polymorphic_embedbecause it defines a new Ecto type that does not fully behave like regular embeds (and cannot, because of limitations in Ecto), hence numerous bug reports on this library such as [this one]( Dan Schultzer: Polymorphic embeds in Ecto ).But you cracked it with this trick:
Now, may I ask if you took a look at
polymorphic_embedbefore writing this blog post, and if so why you didn’t use it in the first place?I’m curious because, if as I suppose correctly and the author of
polymorphic_embeddidn’t know about le trick, then I guess it could be rewritten by deleting 80% of the code and eliminating 80% of the bugsThinking out loud, but this library became unmaintained (it seems), is widely used and is riddled with subtle discrepancies compared with regular Ecto embeds. For those who would need it, a PR with “some” fixes here.
PS : might be a typo here:
Cheers!
danschultzer
I think the typo is actually from Ecto API change, I’m updating it thanks!
Yeah, I did look at the library but it doesn’t really solve polymorphic embeds with Ecto and neither does my small version here. The actual production version I have is more involved so it can handle some normalization, prepare changes, etc. But at the end of the day, embeds have some sharp corners and it only gets worse when you lose the guarantee with the struct which Ecto is built upon. There are a lot of assumptions with data structures in Ecto so I think the only way for polymorphic embeds to work properly is that Ecto needs to be changed in how embeds works. It’s been a while so I can’t remember the exact details, but if you go through the code path of how embeds are dumped/loaded you’ll see that embeds are treated separately from other data and it makes things tricky.
At least having all of this logic in your codebase you can adjust it for your exact needs and work around the sharp corners yourself. You often don’t need everything that the embed type need to be able to do.