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

Igniter messes with sourceror

I have a pretty empty Phoenix 1.8 project where I have added igniter, ash and ash_phoenix, using igniter, without issue. Now I want to add ash_authentication with mix igniter.install ash_authentication --auth-strategy magic_link, however that fails after setting up igniter with: ```...

How to show embedded resource in Ash Admin

I want to create some actions on an embedded resource that I want to call from the Admin UI. Is that possible?

Is there a guide for bringing Ash into an existing phx.gen.auth app?

I've seen a few posts about this in the past but nothing recently. It may be as simple as creating a User resource that matches the Ecto schema and turning automatic migrations off? But then how do I get current_user into the scope/context so that all of the policy stuff works great? Trying to gauge if it's more effort to attempt this or to replace all of the auth (including UI) with AshAuthenticationPhoenix.

Manually sign in a user in Phoenix

I have a homegrown invitation solution where admin users can invite other users. I did an experiment with using the magic link strategy, but my thinking is that I don't want to actually create the user record until the invitation is accepted. The following Phoenix controller handles accepting an invitation: ```elixir defmodule DrengWeb.InvitationController do...
Solution:
This might a bit dirty, but by adding the GenerateTokenChange and borrowing the magic link signin strategy I got my action to work ```diff create :sign_up_with_invitation do argument :token, :string, allow_nil?: false ...

Deeply nested forms

Hi all, you know how Ash supports nested forms? Is there a way to do doubly or triply nested forms? Or is there a different approach to achieve this if this is not natively supported?
Solution:
I think it should be possible, looking at the code, the forms option has another forms field, so you should be able to nest them that way.

Question on Multitenancy.

I read this doc But few things are not still clear for me. I am trying to add Attribute Multitenancy 1. Should I add multitenancy macro and belongs to :organization to all resources? 2. I noticed we have functions like Ash.Query.set_tenant for read and Ash.Changeset.set_tenant for create. So I don't need to add something like change manage_relationship(:organization_id, :organization, type: :append) for create action?...

How to pass `opts` to Repo module functions?

Example:
opts = [settings: [final: 1]]
Repo.get(User, id, opts)
opts = [settings: [final: 1]]
Repo.get(User, id, opts)
Can it be passed through resource actions?...

Adding new create action produces error in resource schema

I have two resources in the same domain, Container, Image and ImageCredentials. Container has a belongs_to relationship with Image, Image has a has_many relationship with Container and a belongs_to relationship with ImageCredentials, while this last one has a has_many relationship with it. Container has a create action (:create_with_nested), with the purpose of creating a new Image in case of a failed match or lookup. I'm fairly new to Ash, and I tried to add the same logic to Image if it cannot match or lookup a ImageCredentials by adding the following :create_with_nested action to Image: ```ex create :create_with_nested do...

How do I do this SQL UPDATE-query in Ash?

I want to update a row in a database table if it exists, and otherwise return an error. In SQL I would do something like this:
UPDATE hats SET activation_token = null, activated = NOW() WHERE activation_token = ...
UPDATE hats SET activation_token = null, activated = NOW() WHERE activation_token = ...
...
Solution:
```elixir require Ash.Query # needed for filter bulk_result = Hats...

Return resource A from an action on resource B

I am working on an invite-only application. I have a User resource and an Invitation resource. I would like to create an :accept_invitation action which validates an invitation record, creates a user, and returns the user. This is what I have attempted so far: ```elixir on the invitation resource update :create_user_with_invitation do...
Solution:
This seems to have done the trick: ```elixir defmodule Dreng.Changes.ValidateInvitation do use Ash.Resource.Change ...

How to properly handle relationships and references

Right now i have this 3 files and to avoid leaving relationships hanging in my delete I manually go through each relationship and delete it. The reason I have the other 2 (update and create) setup the way they're is because I was following this tutorial from the docs, but it doesnt have anything on destroy, and just putting ` change manage_relationship(:ticket_ids...
Solution:
Just had to add
references do
reference :team, on_delete: :delete
reference :ticket, on_delete: :delete
end
references do
reference :team, on_delete: :delete
reference :ticket, on_delete: :delete
end
on the union/intermediary table, and then I can delete teh custom destroy action, heres the final files in case some needs them later...

JSON API patch without bulk update

Is is possible to do a PATCH with ash_json_api that will do an update that doesn't use bulk_update? I'm wanting to write an update policy that's essentially a runtime policy (i.e. it needs access to the object that's changing in order to validate). Poking around source code though, it looks like PATCHes are always implemented with bulk updates?

Conditional Relationship Creation

I'm trying to create a profile when a user registers, but only if one doesn't already exist. I tried using manage_relationship/4 with type: :direct_control and it does create the profile, but if the user already has one - with more data fulfilled - it overwrites it with the partial available data at user registration — which is not ideal. Is manage_relationship/4 the right way to handle this? Or should I use an after_action hook to conditionally create the profile?...
Solution:
That's right, this makes it ```elixir change after_action(fn changeset, user, context -> profile = Ash.load!(user, :profile) |> Map.fetch!(:profile)...

I found possible bug with Ash.DataLayer.Mnesia in form validation

I'm writing the Tunez code using Mnesia, and I came across this difference. ```elixir form = AshPhoenix.Form.for_create(Tunez.Music.Artist, :create) AshPhoenix.Form.validate(form, %{name: "Best Band Ever"}) # returning valid ...

How to implement an override table?

Hey. Business wants to override a table that I'd like to keep free of tampering. So I'm thinking to create a the_table_override that can be merged at some other stage. But of course I'd like to keep the Ash Resources in sync schema wise. So if I chance the main one the other follows. Is there a way to create a new resource that mirrors another one at all times? I imagine macros is the answer, but maybe there's another path?...

Manual relationship in belongs_to

Hi! Is there any reason there is no way to have a manual relationship in a belongs_to block? Is it because that one by default creates the attribute? I'm doing a Stripe wrapper that generates custom dependencies, and some relationships are belongs_to-like and some are has_*-like. And would love to preserve the semantics....

How to concatenate strings on related column like STRING_AGG with sort?

I have a simple has_many relation with parts:
has_many :parts, Part
has_many :parts, Part
...

Idiomatic way to test AshOban trigger conditions

As the title state, what's the best way to test the condition that would trigger an AshOban trigger?

Use full text search in fields

Newbie here, is there a way I can fields defined as full text search fields?