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

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement