How to manage `belongs_to` relations

I have this action in my resource
create :new_token do
accept([:nickname, :scopes, :system, :expires_at])

argument :user, Project.Resources.Accounts.User do
allow_nil?(false)
end

change(manage_relationship(:user, on_match: :relate))

# ...
end
create :new_token do
accept([:nickname, :scopes, :system, :expires_at])

argument :user, Project.Resources.Accounts.User do
allow_nil?(false)
end

change(manage_relationship(:user, on_match: :relate))

# ...
end
but when I create a new token, the user and user_id are nil
|> Ash.Changeset.for_create(:new_token, %{
user: user,
system: true,
scopes: ["read", "write"],
expires_at:
DateTime.utc_now()
|> DateTime.add(@max_age, :second)
|> DateTime.truncate(:second)
})
|> Ash.Changeset.for_create(:new_token, %{
user: user,
system: true,
scopes: ["read", "write"],
expires_at:
DateTime.utc_now()
|> DateTime.add(@max_age, :second)
|> DateTime.truncate(:second)
})
7 Replies
ZachDaniel
ZachDaniel3y ago
Ah, so I think the issue is the on_match option. I believe you want on_lookup: :relate Alternatively, you can use type: :append_and_remove That is the generalized type for "add the new thing, remove the old thing if it exists" without creating or deleting anything (only changing what is related)
harry
harryOP3y ago
Thanks that fixed it 🙌🏼 nearly finished moving everything over to Ash so I can do one mamoth commit today ~60 files changed
ZachDaniel
ZachDaniel3y ago
Thats awesome 😄 Is your project open source?
harry
harryOP3y ago
Sadly not 😞
ZachDaniel
ZachDaniel3y ago
No problem at all, just curious 😄 If you could write a few words on your migration experience that would be really cool. Perhaps posting something in #showcase .
harry
harryOP3y ago
Yeah I'm planning a personal blog post; no company engineering blog yet 🤣
ZachDaniel
ZachDaniel3y ago
excellent! Looking forward to it.

Did you find this page helpful?