Ecto v1.1.0 Ecto.Model.Callbacks

Warning: Ecto callbacks are deprecated.

Summary

Functions

Applies stored callbacks in model to given data

Macros

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

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

Adds a callback that is invoked after the model is loaded from the repository

Adds a callback that is invoked after the model is updated

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

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

Adds a callback that is invoked before the model is updated

Functions

__apply__(module, callback, data)

Applies stored callbacks in model to given data.

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

This function expects a changeset for all callbacks except after_load as input.

Examples

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

Macros

after_delete(function, args \\ [])

Adds a callback that is invoked after 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 and always runs inside a transaction.

Example

after_delete User, :notify_garbage_collectors
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.

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

The callback must return a changeset and always runs inside a transaction.

Example

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

Same as after_insert/2 but with arguments.

after_load(function, args \\ [])

Adds a callback that is invoked after the model is loaded from the repository.

The callback receives the model being loaded and must return a model.

This callback can be useful to resolve the value of virtual fields in situations they must always be present in the model. Since this will be invoked every time the model is loaded, the callback must execute very quickly to avoid drastic perfomance hits.

Another common misuse of after_load callbacks is to use it for loading fields which are not always required. For example, imagine you need to generate an access token based on the User id and password. One could use after_load and a virtual field to precompute the access_token value but it is simpler and cleaner to simply provide an access_token function in the model:

User.access_token(user)

Example

after_load Post, :set_permalink
after_load(module, function, args)

Same as after_load/2 but with arguments.

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 and always runs inside a transaction.

Example

after_update User, :notify_account_change
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.

The callback receives an Ecto.Changeset. At this point, the changeset has already been validated and is always valid.

The callback must return a changeset and always runs inside a transaction.

Example

before_delete User, :copy_to_archive
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.

Since on insert all the model fields plus changeset changes are sent to the repository, the callback receives an Ecto.Changeset with all the model fields and changes in the changeset.changes field. At this point, the changeset has already been validated and is always valid.

The callback must return a changeset and always runs inside a transaction.

Example

before_insert User, :generate_permalink
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.

The callback receives an Ecto.Changeset with the changes to be sent to the database in the changeset.changes field. At this point, the changeset has already been validated and is always valid.

The callback must return a changeset and always runs inside a transaction.

Example

before_update User, :set_update_at_timestamp
before_update(module, function, args)

Same as before_update/2 but with arguments.