Make Changes
Learn how to make changes while working with embedded schemas.
Now, let’s look at making some changes to our embedded records. Since the data is embedded in the parent record, it’s tempting to think that we can simply manipulate the records the same as the other fields in the schema. This is not the case.
Use changesets with embeds
We must always use changesets when working with embeds and the specialized functions Ecto provides, put_embed/4
and cast_embed/3.
The child records are stored in a column in the database, but in our Elixir code, it’s more accurate to think of embeds like associations.
The put_embed/4
and cast_embed/3
keywords work just like their counterparts put_assoc/4
and cast_assoc/3
, so we need to consider the :on_replace
option when setting up our embeds.
The values available for :on_replace
are as mentioned below.
:raise
(the default):mark_as_invalid
:update
:delete
These work the same way as they do in associations. You can flip back to here if you need a refresher on what they are.
Get hands-on with 1200+ tech skills courses.