Convert resources that uses the same table

In my system I have two separated Apis/Contexts, Accounts and Actions. In Accounts I have a User resource where I handle authentication, etc. In Auctions I have a Bidder resource which I use to identify a bidder in a auction. Both of these resources uses the same table "users". My socket will always contain the User structure, not the Bidder one. So, if I want to create a Bid (also a resource inside Auctions), I always need to get the actor (User) id, fetch the bidder from the database, then add the bid. Now, the Bidder only has a subset of the fields that the User has, so technically all the fields needed to fill the Bidder struct is already in the User struct. So I was wondering, is there a standard way to convert the User resource into a Bidder one? I'm mainly asking this because in my Bid create action, I use change relate_actor(:bidder) to fill the relationship (bidder_id which is a fkey to the users table id field), so if I send the User as the actor, Ash will complain and fail the creation.
2 Replies
ZachDaniel
ZachDaniel3y ago
Id suggest writing a custom change and setting bidder_id yourself
change fn changeset, %{actor: actor} ->
Ash.Changeset.force_change_attribute(:bidder_id, actor.id)
end
change fn changeset, %{actor: actor} ->
Ash.Changeset.force_change_attribute(:bidder_id, actor.id)
end
You could put that in your action
Blibs
BlibsOP3y ago
Thank you, that did the trick!

Did you find this page helpful?