I have an resource User
and Organization
in my Accounts
domain.
In User
I have the following many_to_many
relationship:
many_to_many :referred_organizations, Organization do
through OrganizationReferredUser
public? true
source_attribute_on_join_resource :referred_user_id
destination_attribute_on_join_resource :organization_id
end
Here is my OrganizationReferredUser
implementation:
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
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
repo Core.Repo
end
identities do
identity :unique_association, [:referred_user_id, :organization_id]
end
actions do
defaults [:read, :destroy, :create]
end
end
Now I want to reuse the same relationship in another domain called Markets
, but instead of having the same User
resource there, I have one called Offeror
instead which uses the same table.
When I try to create the OrganizationReferredOfferor
in my Markets
domain, the code will not compile in the belongs_to
block belongs_to :referred_offeror, Offeror
because there will be no referred_offeror_id
in my resource and I can’t find an option to tell Ash to actually use the existing (exists in the DB and in the original OrganizationReferredUser
resource, not in this resource) referred_user_id
instead.
How can I achieve that?