Making Changes

Now let’s look at making some changes to our embedded records. Because the data is embedded in the parent record, it’s tempting to think that you can simply manipulate the records the same as the other fields in the schema. But this is not the case. You must always use changesets when working with embeds, and work with the specialized functions that Ecto provides: put_embed/4 and cast_embed/3. In the database, the child records are stored in a column, but in our Elixir code, it’s more accurate to think of embeds like associations.

put_embed/4 and cast_embed/3 work just like their counterparts put_assoc/4 and cast_assoc/3 so we need to give some thought to the :on_replace option when setting up our embeds. The values available ...

Get Programming Ecto now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.