How do you set an attribute to the join resource when creating?
For example, I have a
User
and an Org
, joined by a many_to_many
through a Join Resource UserOrg
.
UserOrg
has an attribute :role
. How do I set the role
on the UserOrg
when creating an Org
?
11 Replies
Read through the options here: https://ash-hq.org/docs/module/ash/latest/ash-changeset#function-manage_relationship-4
There is a format for determining which fields get sent to the join table
although, FWIW, in your case you could just manage the join relationship directly
No such relationship users_join_relationship for resource Org
I tried with users_join_assoc
too, but then it sets that to an empty list.ah, yeah sorry it should be
users_join_assoc
ah, perhaps it needs to be a list not a map?
you should be able to manage it that way though
oh
my example didn't have any options
With
:append_and_remove
it gives an error record with role: :owner | user_id: "EF6DF726A6" not found
.
However, with :create
instead, it gives this error: Input invalid: relationship user is required
.
I'm guessing this is complaining that the user on the join resource is not provided through here.oh, yeah
type: :create
That doesn't work either
Input invalid: relationship user is required.
I assume on the join resource you have
belongs_to :user, ...
set attribute_writable? true
on the belongs_to relationshipsthat works! 🚀
For future readers, after setting
attribute_writable? true
on your join resource association, here's the final create
function which works:
Thanks a lot for all your help @Zach Daniel! :ashley:You probably don’t need the user_id argument anymore
Yes, thanks!