Crecto
Search…
Changeset
The changeset performs validations on data objects. If any changeset is not valid, then the database operation will not happen.
1
user = User.new
2
changeset = User.changeset(user)
3
changeset.valid?
Copied!
Repo operations on single instances will also return a changeset, where you can check is validity and also check for error messages and constraint error messages.
1
user = User.new
2
changeset = Repo.insert(user)
3
changeset.errors.any?
Copied!

Changeset methods

errors

An array of validation and constraint errors

valid?

Returns whether the changeset is valid not

instance

The new object instance after the database operation
1
user = User.new
2
changeset = Repo.insert(user)
3
changeset.instance.id # new database id
Copied!

action

Action performed on the changeset afer the database operation :insert, :update, etc

changes

An array of field changes on the data object

source

A hash of the original data set
Last modified 3yr ago