sezaru

sezaru

After Ash update, calculation with to_many relatioonship stopped working

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.

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

I’ve pushed a fix to main that temporarily disables this validation. Please try it out when you get a chance to confirm that it resolves the issue for you.

Also Liked

zachdaniel

zachdaniel

Creator of Ash

Actually, I will fix it under the hood where we cast the value and store the casted value.

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement