View on GitHub
Define module-level callbacks in models.
A callback is invoked by your `Ecto.Repo` before (or after)
particular events. A callback must always return the given
module and they always run inside a transaction.
defmodule User do
after_create Stats, :increase_user_count
When creating the user, the `after_create` callbacks will be
invoked with the `user` struct as argument. Multiple callbacks
can be defined, they will be invoked in order of declaration.
Callbacks in Ecto are useful for data consistency, for keeping
counters, setting fields 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`.