> For the complete documentation index, see [llms.txt](https://crystal-garage.gitbook.io/lustra-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://crystal-garage.gitbook.io/lustra-docs/model/lifecycle/callbacks.md).

# Triggers

Callbacks let you run code before or after lifecycle events.

```crystal
class User
  include Lustra::Model

  column id : Int64, primary: true, presence: false
  column email : String
  column normalized_email : String?

  before(:validate) do |model|
    user = model.as(User)
    user.normalized_email = user.email.downcase
  end

  after(:create, :send_welcome_email)

  def send_welcome_email
    EmailManager.send_email(email)
  end
end
```

## Block Callbacks

Block callbacks receive `Lustra::Model`, so cast the argument before accessing model-specific methods:

```crystal
before(:validate) do |model|
  user = model.as(User)
  user.email = user.email.strip
end
```

## Method Callbacks

Use the method form when the callback should call a model method:

```crystal
after(:create, :send_welcome_email)
```

The method must be public.

## Events

Lustra supports callbacks around these events:

| Event       | Triggered by                                      |
| ----------- | ------------------------------------------------- |
| `:validate` | `valid?`, `valid!`, and save paths that validate. |
| `:save`     | `save`, `save!`, and association save paths.      |
| `:create`   | Saving a new model with an `INSERT`.              |
| `:update`   | Saving a persisted model with changed columns.    |
| `:destroy`  | `destroy` and `destroy_all`.                      |

Direct lifecycle-bypassing methods such as `delete`, `delete_all`, `update_column`, `update_columns`, `update_all`, `increment!`, `decrement!`, and `touch` do not run model callbacks.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://crystal-garage.gitbook.io/lustra-docs/model/lifecycle/callbacks.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
