Use Changesets to Persist Data
Now that we have schemas, we need a way to insert data into the database. The approach that Ecto takes is significantly different than ActiveRecord, but it’s one of the most loved components of Ecto. Let’s dive into Ecto changesets.
Ecto.Changeset[54] lets us turn data parameters into a persisted database record. Changesets are used for creating new records or updating existing records. We can also validate incoming data using changesets—this ensures our database records match our application’s business rules.
Let’s write our first changeset. We’ll start with the Contact schema because it has fewer fields.
Create Our First Changeset
The functional nature of Elixir is visibly seen in the changeset function we’re ...
Get From Ruby to Elixir 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.