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
zachdaniel
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
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









