In my resource, I have the following many_to_many
relationship:
many_to_many :favorited_by_users, User do
through FavoriteProperty
join_relationship :property_favorites
source_attribute_on_join_resource :property_id
destination_attribute_on_join_resource :user_id
end
I want to have a calculation that will tell if a user favorited that resource:
calculate :favorite?,
:boolean,
expr(exists(property_favorites, user_id == ^actor(:id)))
This used to work fine, but now after updating to 2.15.19
, now I guet this error:
** (Ash.Error.Unknown) Unknown Error
Context: resolving query on fetch Marketplace.Markets.Property.list_user_favorites
* Context: resolving query on fetch Marketplace.Markets.Property.list_user_favorites
** (RuntimeError) Only to-one relationship references are allowed in a calculation reference paths. Got: "property_favorites" in favorite?(%{tracer: nil, actor: #Marketplace.Accounts.User<full_name: #Ash.NotLoaded<:calculation>, organization_roles_as_string: #Ash.NotLoaded<:calculation>, referred_by: #Ash.NotLoaded<:relationship>, created_by: #Ash.NotLoaded<:relationship>, impersonate_permissions: #Ash.NotLoaded<:relationship>, preference: #Ash.NotLoaded<:relationship>, organization: #Ash.NotLoaded<:relationship>, token: #Ash.NotLoaded<:relationship>, __meta__: #Ecto.Schema.Metadata<:loaded, "users">, id: "e1812726-25fe-43df-97d7-68107787b8b0", email: #Ash.CiString<"abe2045@willms.org">, phone_number: "+1 (555) 555-5555", first_name: "Torey", surname: "Torphy", roles: [:super_admin], organization_roles: [:agent], confirmed_at: ~U[2023-10-19 14:49:08.752152Z], active?: true, referral_code: "a9f88a40-98f0-40c2-95b7-9fecbd94a83d", normalized_full_name: "torey torphy", inserted_at: ~U[2023-10-19 14:49:08.746216Z], updated_at: ~U[2023-10-19 14:49:08.753379Z], organization_id: "185e2073-baa8-48f7-ba7b-cc873104a528", created_by_id: nil, referred_by_id: nil, aggregates: %{}, calculations: %{}, ...>, authorize?: true, tenant: nil})
To extract a single value from a to_many relationship or path that includes a to_many relationship, use a `first` aggregate.
The error suggests to use an aggregation, but AFAIK I can’t access the actor in the aggregation.