Ecto.Model.Callbacks

Parsed documentation:
View on GitHub
Define module-level callbacks in models.

## Lifecycle callbacks

Ecto provides lifecycle callbacks around insert, update
and delete commands.

A callback is invoked by your `Ecto.Repo` before (or after)
particular events. Lifecycle callbacks always receive a
changeset as an argument and must always return a modified changeset.

Such callbacks are useful for data consistency: keeping
counters, setting field values and so on. For this reason,
callbacks:

  * cannot abort
  * run inside the transaction (if supported by the database/adapter)
  * are invoked only after the data is validated

Therefore, don't use callbacks for validation, enforcing business
rules or performing actions unrelated to the data itself, like
sending e-mails.

Finally keep in mind callbacks are not invoked on bulk actions
such as `Ecto.Repo.update_all/3` or `Ecto.Repo.delete_all/2`.

## Other callbacks

Besides lifecycle callbacks, Ecto also supports an `after_load`
callback that is invoked everytime a model is loaded with the
model itself. See `after_load/2` for more informations.

## Examples

    defmodule User do
      use Ecto.Model.Callbacks

      after_insert :increase_user_count

      def increase_user_count(changeset) do
        # ...
      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/1
    after_insert :increase_user_count

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

    # Invoke the remote function Stats.increase_user_count/1
    after_insert Stats, :increase_user_count

    # Invoke the remote function Stats.increase_user_count/3
    # with the given arguments (changeset is prepended)
    after_insert Stats, :increase_user_count, ["foo", "bar"]
No suggestions.
Please help! Open an issue on GitHub if this assessment is incorrect.