Short short version: insert == build + Repo.insert. That’s the expected behavior.
Functions like Resource.changeset are not used by ExMachina, it uses its own casting machinery:
You didn’t include any stacktrace in your original post about a CaseClauseError, but I’d guess the one it printed pointed to this code, which is expecting either maps or nils in an association:
Some of both? It’s complicated.
I personally never found the “make an object graph but don’t persist it” feature useful even in ExMachina’s Ruby predecessor (FactoryBot), and that feature is even trickier in Elixir where records can’t be aliased (referred to in multiple places but saved in only one).
For instance, trying to make an unsaved Resource with the same unsaved user listed as both user and one of the authors will lead to sadness - the first User would insert correctly, but the second would fail uniqueness validation. ![]()
That wouldn’t be a problem in the Ruby version since resource.authors[0] and resource.user could literally point to the same object that gets mutated on save - one of the few cases where mutable state’s sneaky-action-at-a-distance is actually helpful.






















