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
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
Hey folks,
I just published a post about Hologram’s funding and where the project goes next - the short version:
Curiosum as Main Spons...
New
A while back, I had to process millions of database updates in a legacy system that was already hitting its 64 GB RAM limit, so scaling t...
New
At the heart of every Phoenix application is the often “invisible” HTTP server layer.
For over a decade Cowboy has served the community ...
New
I recently figured out how to the the Rust hotpath profiling crate running in an elixir benchmark script (for profiling NIFs). I had some...
New
I’ve published Part 3 of my Elixir distributed systems learning series.
This part explores process monitoring using the low-level primit...
New
The Phoenix framework is notorious for its long term stability and dependability. Unlike most comparable projects, the Phoenix team activ...
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #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.