Ecto.Changeset.change
change
, go back to Ecto.Changeset module for more information.
Specs
change( Ecto.Schema.t() | t() | {data(), types()}, %{required(atom()) => term()} | Keyword.t() ) :: t()
Wraps the given data in a changeset or adds changes to a changeset.
changes
is a map or keyword where the key is an atom representing a
field, association or embed and the value is a term. Note the value
is
directly stored in the changeset with no validation whatsoever. For this
reason, this function is meant for working with data internal to the
application.
When changing embeds and associations, see put_assoc/4
for a complete
reference on the accepted values.
This function is useful for:
- wrapping a struct inside a changeset
- directly changing a struct without performing castings nor validations
- directly bulk-adding changes to a changeset
Changed attributes will only be added if the change does not have the same value as the field in the data.
When a changeset is passed as the first argument, the changes passed as the second argument are merged over the changes already in the changeset if they differ from the values in the struct.
When a {data, types}
is passed as the first argument, a changeset is
created with the given data and types and marked as valid.
See cast/4
if you'd prefer to cast and validate external parameters.
Examples
iex> changeset = change(%Post{})
%Ecto.Changeset{...}
iex> changeset.valid?
true
iex> changeset.changes
%{}
iex> changeset = change(%Post{author: "bar"}, title: "title")
iex> changeset.changes
%{title: "title"}
iex> changeset = change(%Post{title: "title"}, title: "title")
iex> changeset.changes
%{}
iex> changeset = change(changeset, %{title: "new title", body: "body"})
iex> changeset.changes.title
"new title"
iex> changeset.changes.body
"body"