I have the following resource that links two other resources:
defmodule Core.Marketplace.Accounts.OrganizationReferredUser do
@moduledoc false
alias Core.Marketplace.Accounts
use Ash.Resource,
domain: Core.Marketplace.Accounts,
data_layer: AshPostgres.DataLayer
code_interface do
define :read
define :create
end
attributes do
uuid_v7_primary_key :id
attribute :metadata, :map do
allow_nil? false
default %{}
end
timestamps()
end
relationships do
alias Accounts.{Organization, User}
belongs_to :referred_user, User
belongs_to :organization, Organization
end
postgres do
table "organization_referred_users"
references do
reference :referred_user, on_update: :update, on_delete: :delete
reference :organization, on_update: :update, on_delete: :delete
end
migration_defaults metadata: "%{}"
repo Core.Repo
end
identities do
identity :unique_association, [:referred_user_id, :organization_id]
end
actions do
defaults [:read, :destroy]
create :create do
primary? true
accept [:referred_user_id, :organization_id, :metadata]
upsert? true
upsert_fields []
upsert_identity :unique_association
end
end
end
To add the resource, I use this in a change:
Ash.Changeset.manage_relationship(
changeset,
:referred_organizations,
[actor.organization_id],
type: :append
)
Now I wan’t to also set the metadata
field when creating that relationship, but I can’t figure out how to add that param to the manage_relationship
call.