set action argument in form

I have two resources: User and Account I'm creating an AshFrom and use that to create an User. On submit, just before creating the User i need to create an account and pass it as argument to the action used in the for_create of the form. Unfortunately i can't manage to find a way to set the argument in the form before it calls the action
7 Replies
Sangres
SangresOP2y ago
in the action i have something like this
argument :account_id, AshUUID.UUID, allow_nil?: false
change manage_relationship(:account_id, :account, type: :append_and_remove)
argument :account_id, AshUUID.UUID, allow_nil?: false
change manage_relationship(:account_id, :account, type: :append_and_remove)
While in the submit i have this
account = Core.Account.create!()

case AshPhoenix.Form.submit(form, before_submit: fn changeset ->
Ash.Changeset.set_argument(changeset, :account_id, account.id)
end) do
...
end
account = Core.Account.create!()

case AshPhoenix.Form.submit(form, before_submit: fn changeset ->
Ash.Changeset.set_argument(changeset, :account_id, account.id)
end) do
...
end
ZachDaniel
ZachDaniel2y ago
🤔 I might suggest making it a different action on the user that creates the account and the user
create :create_with_account do
# account_id normally required, but not in this action
allow_nil_input [:account_id]
argument :some_account_attribute, :some_type

change before_action(fn changeset ->
account = Core.Account.create!(...)
Ash.Changeset.change_attribute(changeset, :account_id, account.id)
end)
end
create :create_with_account do
# account_id normally required, but not in this action
allow_nil_input [:account_id]
argument :some_account_attribute, :some_type

change before_action(fn changeset ->
account = Core.Account.create!(...)
Ash.Changeset.change_attribute(changeset, :account_id, account.id)
end)
end
Sangres
SangresOP2y ago
Unfortunately i tried using before_action and before_transaction but its not working. Could the problem be caused by the fact that account is the resource that manages the multi tenancy in my app?
ZachDaniel
ZachDaniel2y ago
That could be the reason, yes 🙂 what kind of error are you getting? I've had a similar type of action before
create :create_with_account do
# account_id normally required, but not in this action
allow_nil_input [:account_id]
argument :some_account_attribute, :some_type

change before_action(fn changeset ->
account = Core.Account.create!(...)
changeset
|> Ash.Changeset.change_attribute(:account_id, account.id)
|> Ash.Changeset.set_tenant(...tenant from account)
end)
end
create :create_with_account do
# account_id normally required, but not in this action
allow_nil_input [:account_id]
argument :some_account_attribute, :some_type

change before_action(fn changeset ->
account = Core.Account.create!(...)
changeset
|> Ash.Changeset.change_attribute(:account_id, account.id)
|> Ash.Changeset.set_tenant(...tenant from account)
end)
end
You might just need to set the tenant? Might not work though, our "tenant is required" validation might cause problems I remember what I did for this in the past. I had an action on the tenant resource that could also create a user
create :create_with_first_user do
argument :username, ...
argument :password, ...


end
create :create_with_first_user do
argument :username, ...
argument :password, ...


end
and then I created the user in an after_action hook on that action
Sangres
SangresOP2y ago
unfortunately i can't manage to make it work. I know that probably there's something im missing on my side. I have now changed the structure of the app to make an user unrelated to the account and now it's working. Thanks a lot for all the info nonetheless ❤️
ZachDaniel
ZachDaniel2y ago
😭sorry it didn’t work out!
Sangres
SangresOP2y ago
no worries 😄

Did you find this page helpful?