| name | ash-framework |
| description | Comprehensive Ash framework guidelines for Elixir applications. Use when working with Ash resources, domains, actions, queries, changesets, policies, calculations, or aggregates. Covers code interfaces, error handling, validations, changes, relationships, and authorization. Read documentation before using Ash features - do not assume prior knowledge. |
Ash Framework Guidelines
Ash is a declarative framework for modeling domains with resources. Read documentation before using features.
Code Interfaces
Define code interfaces on domains - avoid direct Ash.get!/2 calls in LiveViews/Controllers:
# In domain
resource Post do
define :get_post, action: :read, get_by: [:id]
define :list_posts, action: :read
define :create_post, action: :create, args: [:title]
end
# Usage - prefer query option over manual Ash.Query building
posts = MyApp.Blog.list_posts!(
query: [filter: [status: :published], sort: [published_at: :desc], limit: 10],
load: [author: :profile, comments: [:author]]
)
post = MyApp.Blog.get_post!(id, load: [comments: [:author]])
Authorization functions are auto-generated: can_create_post?(actor), can_update_post?(actor, post).
Using scopes: Pass scope: socket.assigns.scope in LiveViews; use context parameter in hooks.
Actions
- Create specific, well-named actions (not generic CRUD)
- Put business logic inside action definitions
- Use
before_action/after_action for same-transaction logic
- Use
before_transaction/after_transaction for external calls
actions do
create :sign_up do
argument :invite_code, :string, allow_nil?: false
change set_attribute(:joined_at, &DateTime.utc_now/0)
change relate_actor(:creator)
end
end