amos-kibet

amos-kibet

Passing other attributes to actions used by AshStateMachine

I have an invitation resource that has a status, user_id among other attributes. I use AshStateMachine to update the status attribute, to accepted when the invited user registers in the app, and to expired, when the invitation expires.
When the invitee registers, I need to save their user ID in the invitation.user_id field, as well as transition the invitation status to accepted. I do it this way, in an after_action hook:

%{status: :success} =
  Invitation.update(invitation, %{user_id: user_id}, authorize?: false)

%{status: :success} = Invitation.accept(invitation, authorize?: false)

This works, but I was wondering if it is possible to pass the user_id to the Invitation.accept/2 action, which is used by AshStateMachine, so that I don’t have to call a separate action (Invitation.update/3) to update the user ID field.

Most Liked

almirsarajcic

almirsarajcic

Can you accomplish that with the :actor option?

zachdaniel

zachdaniel

Creator of Ash

Agreed that using the actor would be the most natural way. With that said, you can definitely just add accept [:user_id] to the action you’re using to transition the state.

Then Invitation.accept(invitation, %{user_id: user_id}, ...)

Additionally, you can add it to the args of the code interface.

define :accept, args: [:user_id]
Invitation.accept( invitation, user_id, ....)

If you do it with the actor, though:

change relate_actor(:user) # set the user to the current actor

then you can just Invitation.accept(invitation, actor: user)

amos-kibet

amos-kibet

Thanks @almirsarajcic and @zachdaniel. Both solutions work!

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Other popular topics Top

chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New

We're in Beta

About us Mission Statement