Ash Framework

AF

Ash Framework

The Elixir backend framework for unparalleled productivity. Declarative tools that let you stop wasting time. Use with Phoenix LiveView or build APIs in minutes for your front-end of choice.

Join

support

showcase

today-i-learned

How to (temporarily) disable data layer entirely

I know this looks weird, but - given a starter project, I would love to properly configure AshPostgres, and then to "comment out" it and swap with an in-memory (non actually persistent) alternative, e.g. Ash.DataLayer.Ets (even better if any accidental entry gets deleted in 24hrs). Use case: I just want to start and deploy a simple static HTML page that requires no logic, nor authentication. In the near future, as I develop features, I want to put them back in. But in the meantime, I want to deploy my app without actually spending $38 per month for a postgres db I would not hit (yes, I'm using fly.io). If I'm tackling this problem wrong, or if you suggest a better workflow, I'm all ears!...
Solution:
But also if you're just not going to be using the resources at all, you can just not start your repo

Good way to (atomically?) persist a file when creating a record?

I'm working on a little project where one of the features is the ability to upload a file (using the :upload action which currently takes a :name and a :path as its arguments), which persists the file to disk and creates a File record in the DB. I want to do this in such a way that that I never end up with files on the disk that the DB doesn't know about, but also that no record is created if the file persistence has failed. How would I achieve this?...

Warning on compare to `nil` in short-circuited query

I have a query with an expression like this: ```elixir expr( is_nil(^start_date) or ^start_date <= fragment("date(?)", completed_date)...

Ash policies fail when they shouldn't

heres the logs ```elixir Parameters: %{"form" => %{"role" => "admin"}} [error] SimpleDemo.Accounts.User.change_role ...

Reducing some code duplication in Ash enums

I find myself defining a few enum modules in my application like this, ```elixir defmodule MyApp.MyDomain.SomeEnum do use Ash.Type.Enum, values: [:foo, :bar, :blah] ...
Solution:
I added some functions to Enum awhile back for this. Pretty sure there are some examples in the docs. https://hexdocs.pm/ash/Ash.Type.Enum.html#module-value-labels-and-descriptions...

How can I sum all the expenses.amount across all categories

I have a Category resource inside a Wallet domain. The Category has an attribute monthly_budget of type :money. Each category has many expenses, and each expense has an amount (also :money). On a domain level (in Wallet):...

Ash policies breakdown not showing help_text

I have this policy (check if the role theyre trying to change the user into is super_admin and if theyre not a super_admin themselves block them) ```policy action(:change_role) do forbid_if expr( args(:role) == :super_admin && ^actor(:role) != :super_admin...
Solution:
fixed

Distinguished if the user was created or if they already existed with a magic link

I would like to perform some actions once the user has been created via a Magic Link, for example sending a welcome email and initializing other resources in other domains. However, I don't see how to distinguish between creation and reading in an after_action. According to my tests, the values of the parameters are identical in both cases. Any ideas?...
Solution:
Here is what works for me: ``` upsert_fields [:email, :last_signed_in_at] ...

How to run custom resource extension before validators

Hi there, I'm writing my first Ash.Resource extension and having some issues I don't know how to solve. This is my simple extension so far: ```ex...
Solution:
You can do this in your transformer: ```elixir def before?(Ash.Resource.Transformers.DefaultAccept), do: true def before?(_), do: false...

Can I aggregate over a calculated field?

I have a File resource, that has calculation :extension, I then have another resource called Attachment, that has_many :files. Is it possible to do this: ``` first :extension, :file, :extension do...

Seed Resources with many-to-many relationships

Hello, I want to seed resources with many-to-many relationships. How would I accomplish this? I am thinking of doing something silly, just reference the rows that were inserted, considering that I don't know the actual IDs, and then read them appropriately and bulk-create them. Any ideas are welcome!...
Solution:
changeset generators. ended up doing what I said above, I basically created my own structs with the indices that I am interested in, and then just used Enum.at(foos, foo_index) to create the correct references. I also had to make the relationships on the many-to-many public so that I can create them. Anywho, thanks for the help!...

Regex validation error

Getting this error when validating:
validate match(:code, "^[A-Za-z0-9]+$/")
validate match(:code, "^[A-Za-z0-9]+$/")
```...

Generated attributes on update?

I've got a table using a generated value (GENERATED ALWAYS) which can change on updates. generated? true only appears to affect inserts. I'm guessing there is a toggle somewhere to handle this maybe? Even if there's just a way to tell an update action, "Hey, definitely get the return value on the update for this one," that would also be perfect. I didn't see any obvious way in the DSLs but... that doesn't mean much, lol....

(ArgumentError) could not fetch application environment :ash_domains for application :test

I just ran the startup script, here: https://ash-hq.org/#get-started But after pasting the command, I straight up get this error message ``` [info] == Migrated 20250829135349 in 0.0s...
Solution:
Please open issues describing the exact steps to reproduce each issue and I will look into it. You should be able to install packages into a new app one at a time to avoid the issues for now.

How to allow only one action and forbid all others by default in policy?

From the doc when multiple policies apply to the same request, all applicable policies must pass for the action to be authorized. I have a policy for the :profile read action, that is available only for the current actor: ```...
Solution:
forbid is the default, if no policy block applies to the action, so you can just remove this one ```elixir policy always() do forbid_if always()...

Resource actions - Transaction

Hi! I am currently working on some :create actions where I have SQLite3 as the datalayer. I have both before_action, manage_relatonship and after_action and I was expecting the whole create action to fail and the transaction to rollback if any of the changes failed. But this does not seem to be the case..?...

graphql subscription triggered by association

I have this subscription on the user resource ``` subscriptions do pubsub __.Endpoint...
Solution:
At the moment I think the only way would be to have a change that updates the user resource as well to trigger the subscription or have 2 separate subscriptions on the client.

Sorting on the first relationship in an aggregate

I want to do an aggregate over a m2m table where the order is important, so I write the code below but whatever I do I can't seem to sort on the first table. Should I be able to do that? ```elixir aggregates do list :phone_numbers_list, [:some_many_to_one_table, :user], :phone_number do...
Solution:
tldr: Calculations ❤️ The solution was to use a calculation on :some_many_to_one_table that got the phone number: ```elixir...

many to many relationship + PhoenixForm + checkboxes

I got the form working - eg; it saves the data in the many-to-many table when I submit the form - using a multi select field. Now I'm trying to figure out how to use checkboxes to create the form. I'm having a hard time to understand how I need to declare the checkboxes so that ash can understand which checked checkbox represents what record. I appreciate any sugestions, or pointers to reading materials - tutorials, articles, docs....

AshAuthentication failures with custom LiveViews

Hello, I want to have my own custom LiveViews for my auth routes, and I read https://alembic.com.au/blog/customising-ash-authentication-with-phoenix-liveview which was very helpful. I am aware of the overrides, but I don't really like them. Here are some relevant code snippets: ```ex...
Solution:
I just figured it out 🙂 If someone could explain why the following worked, that would be awesome! The issue was that originally I couldn't sign in nor register....