Ecto.Model.Callbacks

Define module-level callbacks in models.

A callback is invoked by your Ecto.Repo before (or after) particular events. Callbacks receive changesets, must always a changeset back and always run inside a transaction.

Example

defmodule User do
  use Ecto.Model.Callbacks

  after_insert Stats, :increase_user_count

  def increase_user_count(changeset)
    # ...
  end
end

When creating the user, the after_insert callbacks will be invoked with a changeset as argument. Multiple callbacks can be defined, they will be invoked in order of declaration.

A callback can be defined in the following formats:

# Invoke the local function increase_user_count
after_insert :increase_user_count

# Invoke the local function increase_user_count
# with the given arguments (changeset is prepended)
after_insert :increase_user_count, ["foo", "bar"]

# Invoke the remote function increase_user_count
after_insert Stats, :increase_user_count

# Invoke the remote function increase_user_count
# with the given arguments (changeset is prepended)
after_insert Stats, :increase_user_count, ["foo", "bar"]

Usage

Callbacks in Ecto are useful for data consistency, for keeping counters, setting changes and so on. Avoid using callbacks for business rules or doing actions unrelated to the data itself, like sending e-mails.

Finally, keep in mind callbacks are not invoked on bulk actions such as Repo.delete_all or Repo.update_all.

Source

Summary

__apply__(module, callback, changeset)

Applies stored callbacks in model to given data

after_delete(function, args \\ [])

Adds a callback that is invoked before the model is deleted from the repository

after_delete(module, function, args)

Same as after_delete/2 but with arguments

after_insert(function, args \\ [])

Adds a callback that is invoked after the model is inserted into the repository

after_insert(module, function, args)

Same as after_insert/2 but with arguments

after_update(function, args \\ [])

Adds a callback that is invoked after the model is updated

after_update(module, function, args)

Same as after_update/2 but with arguments

before_delete(function, args \\ [])

Adds a callback that is invoked before the model is deleted from the repository

before_delete(module, function, args)

Same as before_delete/2 but with arguments

before_insert(function, args \\ [])

Adds a callback that is invoked before the model is inserted into the repository

before_insert(module, function, args)

Same as before_insert/2 but with arguments

before_update(function, args \\ [])

Adds a callback that is invoked before the model is updated

before_update(module, function, args)

Same as before_update/2 but with arguments

Functions

__apply__(module, callback, changeset)

Applies stored callbacks in model to given data.

Checks wether the callback is defined on the model, returns the data unchanged if it isn't.

This function expects a changeset as input.

Examples

iex> changeset = Ecto.Changeset.cast(params, %User{}, ~w(name), ~w())
iex> Ecto.Model.Callbacks.__apply__ User, :before_delete, changeset
%Ecto.Changeset{...}
Source

Macros

after_delete(function, args \\ [])

Adds a callback that is invoked before the model is deleted from the repository.

The callback receives an Ecto.Changeset with the model stored in it. The callback must return a changeset.

Example

after_delete User, :notify_garbage_collectors
Source
after_delete(module, function, args)

Same as after_delete/2 but with arguments.

Source
after_insert(function, args \\ [])

Adds a callback that is invoked after the model is inserted into the repository.

The callback receives an Ecto.Changeset with both repository values and changeset changes already applied to the model. The callback must return a changeset.

Example

after_insert Stats, :increase_user_count
Source
after_insert(module, function, args)

Same as after_insert/2 but with arguments.

Source
after_update(function, args \\ [])

Adds a callback that is invoked after the model is updated.

The callback receives an Ecto.Changeset with both repository values and changeset changes already applied to the model. The callback must return a changeset.

Example

after_update User, :notify_account_change
Source
after_update(module, function, args)

Same as after_update/2 but with arguments.

Source
before_delete(function, args \\ [])

Adds a callback that is invoked before the model is deleted from the repository.

The callback receives an Ecto.Changeset. Although the changes field is ignored by the repository on delete, future functionality will allow pre-conditions to be set on delete.

Example

before_delete User, :copy_to_archive
Source
before_delete(module, function, args)

Same as before_delete/2 but with arguments.

Source
before_insert(function, args \\ [])

Adds a callback that is invoked before the model is inserted into the repository.

Since on insert all the model fields plus changeset changes are sent to the repository, the callback will receive an Ecto.Changeset with all the model fields and changes in the changeset.changes field. The callback must return a changeset.

Example

before_insert User, :generate_permalink
Source
before_insert(module, function, args)

Same as before_insert/2 but with arguments.

Source
before_update(function, args \\ [])

Adds a callback that is invoked before the model is updated.

The callback receives an Ecto.Changeset with the changes to be sent to the database in the changeset.changes field. The callback must return a changeset.

Example

beforeupdate User, :setupdateattimestamp

Source
before_update(module, function, args)

Same as before_update/2 but with arguments.

Source