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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.