Ecto.Model.Callbacks

Define module-level callbacks in models.

A callback is invoked by your Ecto.Repo before (or after) particular events. Callbacks always run inside a transaction.

Example

defmodule User do
  use Ecto.Model.Callbacks

  before_create User, :set_default_fields
  after_create Stats, :increase_user_count

  def set_default_fields(user)
    # ...
  end
end

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

Important

As callbacks can be used to alter the model, please make sure to always return the model struct, even when unaltered.

Callbacks will not be invoked on bulk actions such as Repo.delete_all or Repo.update_all.

Source

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

__apply__(model, callback)

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"}
Source

Macros

after_delete(module, function)

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
Source
after_insert(module, function)

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
Source
after_update(module, function)

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
Source
before_delete(module, function)

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
Source
before_insert(module, function)

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
Source
before_update(module, function)

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

Source