Associations Using External Data
Explore how to create and manage changesets with associations when working with external data in Ecto. Learn to use cast_assoc for handling nested data from forms, APIs, or uploads, and understand how to define changeset functions to support creating or updating associated records efficiently.
We'll cover the following...
We’ll now look at how to create a changeset with associations when the data comes from an external source. We will use the following:
- Forms presented to the user
- Uploaded spreadsheet
- Data posted to an API endpoint and so on
Earlier, we saw that we create new changesets using change or cast, depending on where the data came from. Similarly, we use put_assoc for internal data (as we saw in the last section) or cast_assoc for external data. This section will look at cast_assoc in more depth.
Creating new Records with associations
Let’s start with the case of adding a new parent record with new child records. As we did when we learned about the cast, we’ll assume that ...