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:
The CreateUserFromInvitation
change looks like this:
My understanding was that the return value of the after_action
beccomes the return value of the whole action, but this test fails:
And in the stack trace it is because the returned struct is an Invitation
.
Is what I'm trying to do (a) possible, and (b) reasonable, or should I go about it in some other way?Solution:Jump to solution
This seems to have done the trick:
```elixir
defmodule Dreng.Changes.ValidateInvitation do
use Ash.Resource.Change
...
GitHub
Update before_action docs to use force_change_attribute/2 by jakobs...
Using change_attribute/2 in a before_action hook triggers a warning that the changeset has already been validated, suggests a new pattern that isn't compatible with the before_action hook. ...
5 Replies
I don’t think it’s possible
So maybe take the invitation token as an argument to a
:create
action on the user instead and do the invitation lookup and validation in a before_action
?that would work
Solution
This seems to have done the trick:
Was a little surprised to see the warning when using
change_attribute
instead of force_change_attribute
, so opened a PR with a suggestion of updating the docs as well: https://github.com/ash-project/ash/pull/2258GitHub
Update before_action docs to use force_change_attribute/2 by jakobs...
Using change_attribute/2 in a before_action hook triggers a warning that the changeset has already been validated, suggests a new pattern that isn't compatible with the before_action hook. ...
neat! thanks for clarifying that ❤️