Ecto.Model.Callbacks
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.
Example
defmodule User do
use Ecto.Model.Callbacks
after_create Stats, :increase_user_count
def increase_user_count(user) # ... endend
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.
Usage
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
.
Summary↑
__apply__(model, callback) | Applies stored callbacks to a model |
after_delete(module, function) | Adds a callback to the model that is invoked before the model is deleted from the database |
after_insert(module, function) | Adds a callback to the model that is invoked after the model is inserted into the database |
after_update(module, function) | Adds a callback to the model that is invoked after the model is updated |
before_delete(module, function) | Adds a callback to the model that is invoked before the model is deleted from the database |
before_insert(module, function) | Adds a callback to the model that is invoked before the model is inserted into the database |
before_update(module, function) | Adds a callback to the model that is invoked before the model is updated |
Functions
Applies stored callbacks to a model.
Checks wether the callback is defined on the model, returns the model unchanged if it isn't.
Examples
iex> Ecto.Model.Callbacks.__apply__ %User{}, :before_create
%User{some_var: "has changed"}
Macros
Adds a callback to the model that is invoked before the model is deleted from the database.
Takes the module and the function that are to be invoked as parameters.
Example
after_delete UserMailer, :send_questioneer
Adds a callback to the model that is invoked after the model is inserted into the database.
Takes the module and the function that are to be invoked as parameters.
Example
after_insert Stats, :increase_user_count
Adds a callback to the model that is invoked after the model is updated.
Takes the module and the function that are to be invoked as parameters.
Example
after_update User, :notify_account_change
Adds a callback to the model that is invoked before the model is deleted from the database.
Takes the module and the function that are to be invoked as parameters.
Example
before_delete User, :copy_to_archive
Adds a callback to the model that is invoked before the model is inserted into the database.
Takes the module and the function that are to be invoked as parameters.
Example
before_insert User, :generate_permalink
Adds a callback to the model that is invoked before the model is updated.
Takes the module and the function that are to be invoked as parameters.
Example
beforeupdate User, :setupdateattimestamp