Ecto Changesets: Difference between revisions

From ElixirBlocks
Jump to: navigation, search
No edit summary
No edit summary
 
Line 7: Line 7:


Changesets can be created using the data from a table schema.  
Changesets can be created using the data from a table schema.  
==Using Ecto.Changeset.cast ==


Ecto.Changeset.cast(%Friends.Person{name: "Bob"}, %{"name" => "Jack"}, [:name, :age])
Ecto.Changeset.cast(%Friends.Person{name: "Bob"}, %{"name" => "Jack"}, [:name, :age])
Line 16: Line 18:
* The names of the fields that you are allowed to change (as a list of atoms)
* The names of the fields that you are allowed to change (as a list of atoms)
*
*
==Using Ecto.Changeset.change ==

Latest revision as of 20:38, 8 October 2023

This page is in progress


Ecto.Repo.insert/2, update/2 and delete/2 require a changeset as their first parameter. The creating, updating or deleting of table data always requires a changeset.

Changesets can be created using the data from a table schema.

Using Ecto.Changeset.cast

Ecto.Changeset.cast(%Friends.Person{name: "Bob"}, %{"name" => "Jack"}, [:name, :age])

The previous code takes three arguments.

  • The Schema
  • the change you want to make to the data
  • The names of the fields that you are allowed to change (as a list of atoms)

Using Ecto.Changeset.change