FlyingNoodle

FlyingNoodle

Return a struct of a given type from a calculation?

Given this code:

relationships do
    belongs_to :sender, MyApp.Accounts.User,
      source_attribute: :sender_id,
      destination_attribute: :id,
      allow_nil?: false,
      attribute_writable?: true

    belongs_to :recipient, MyApp.Accounts.User,
      source_attribute: :recipient_id,
      destination_attribute: :id,
      allow_nil?: false,
      attribute_writable?: true

    has_many :messages, MyApp.Conversations.Message
  end

  calculations do
    calculate :other_user,
              :struct,
              expr(
                if sender_id == ^arg(:current_user_id) do
                  recipient
                else
                  sender
                end
              ),
              constraints: [instance_of: MyApp.Accounts.User] do
      argument :current_user_id, :uuid, allow_nil?: false
    end
  end

I would like the calculation other_user to return an instance of the MyApp.Accounts.User resource. However, I get the error that: error: undefined function calculate/5 (there is no such import).

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

So, you won’t be able to this using an expression calculation, you’ll need to use an Elixir calculation. This is because expression calculations done in the data layer can’t return related resources, only relationships and runtime calculations can do that.

defmodule RecipientOrSender do
  use Ash.Calculation

  def load(_, _, _), do: [:recipient, :sender]
  
  def calculate(records, _, context) do
     Enum.map(records, fn record -> 
       if record.recipient.id == context.current_user_id do
         record.recipient
       else
         record.sender
       end
     end)
  end
end

Then you can do:

    calculate :other_user,
              :struct,
              expr(
                if sender_id == ^arg(:current_user_id) do
                  recipient
                else
                  sender
                end
              ) do
      constraints [instance_of: MyApp.Accounts.User]
      argument :current_user_id, :uuid, allow_nil?: false
    end

Although, come to think of it, you could use a manual relationship, assuming you’re okay with using the actor in the standard way as opposed to passing in a current_user_id.

defmodule OtherUser do
  use Ash.Resource.ManualRelationship

  def load(records, _, %{api: api, actor: %{id: actor_id}}) do
    records
    |> Api.load!([:sender, :recipient])
    |> Map.new(fn record -> 
      {record.id, [record.sender, record.recipient] |> Enum.filter(&(&1.id == actor_id))}
    end)
  end

  def load(_, _, _), do: []
end

Haven’t actually run the above code snippets, they are just examples to get you going.

Also Liked

FlyingNoodle

FlyingNoodle

This was almost right, This is correct:

def load(records, _, %{api: api, actor: %{id: actor_id}}) do
    {:ok, records
    |> MyApi.load!([:sender, :recipient])
    |> Map.new(fn record -> 
      {record.id, [record.sender, record.recipient] |> Enum.filter(&(&1.id == actor_id))}
    end)}
  end

I was most worried about this making one query per result but it actually doesn’t and works very nicely!

[debug] QUERY OK source="users" db=0.3ms idle=1855.7ms
SELECT u0."id", u0."email", u0."hashed_password" FROM "users" AS u0 LIMIT $1 [1]
↳ AshPostgres.DataLayer.run_query/2, at: lib/data_layer.ex:648
[debug] QUERY OK source="conversations" db=0.5ms idle=1856.8ms
SELECT c0."id", c0."sender_id", c0."recipient_id" FROM "conversations" AS c0 WHERE ((c0."sender_id"::uuid = $1::uuid) OR (c0."recipient_id"::uuid = $2::uuid)) ["d065715d-14fb-4eeb-9e4c-867b41e7ea18", "d065715d-14fb-4eeb-9e4c-867b41e7ea18"]
↳ AshPostgres.DataLayer.run_query/2, at: lib/data_layer.ex:648
[debug] QUERY OK source="users" db=1.2ms idle=1859.4ms
SELECT u0."id", u0."email", u0."hashed_password" FROM "users" AS u0 WHERE (u0."id"::uuid IN ($1::uuid,$2::uuid,$3::uuid,$4::uuid)) ["80cef98a-ed05-4955-9fce-114b54467d4c", "834e6d70-7a48-4065-ae1c-bc8f6c8fbc0e", "99c67645-2208-4185-9961-7517d4779222", "d065715d-14fb-4eeb-9e4c-867b41e7ea18"]
↳ AshPostgres.DataLayer.run_query/2, at: lib/data_layer.ex:648
[debug] QUERY OK source="users" db=0.4ms queue=0.3ms idle=1860.1ms
SELECT u0."id", u0."email", u0."hashed_password" FROM "users" AS u0 WHERE (u0."id"::uuid IN ($1::uuid,$2::uuid,$3::uuid,$4::uuid,$5::uuid,$6::uuid,$7::uuid,$8::uuid,$9::uuid,$10::uuid,$11::uuid,$12::uuid,$13::uuid,$14::uuid,$15::uuid,$16::uuid,$17::uuid,$18::uuid,$19::uuid,$20::uuid,$21::uuid,$22::uuid,$23::uuid,$24::uuid,$25::uuid,$26::uuid,$27::uuid,$28::uuid,$29::uuid,$30::uuid,$31::uuid,$32::uuid,$33::uuid,$34::uuid,$35::uuid,$36::uuid,$37::uuid,$38::uuid,$39::uuid,$40::uuid,$41::uuid,$42::uuid,$43::uuid,$44::uuid,$45::uuid,$46::uuid,$47::uuid,$48::uuid,$49::uuid,$50::uuid,$51::uuid,$52::uuid,$53::uuid,$54::uuid,$55::uuid,$56::uuid,$57::uuid,$58::uuid,$59::uuid,$60::uuid,$61::uuid,$62::uuid,$63::uuid)) ["4e42a0a2-f982-422a-bef3-f77bc3fab5ed", "fbee0b56-50a6-45fb-b9e0-b48103f82f61", "2c97d01a-c00b-4186-9ac2-70f649cad75b", "5150363b-d8cd-4f41-9c81-06b3bcc5faae", "6cfbfa16-4283-478b-bb7e-9d39ddc02c4e", "ce9afdd2-6575-4422-ac4d-b3ffe34aab5c", "941a544a-0c7c-4a72-a531-261706bd6b41", "f8b927a5-27b4-4258-8fef-87b06aec40af", "80cef98a-ed05-4955-9fce-114b54467d4c", "2febf7fc-2e7e-4d10-8e8e-42980bfee682", "4cee3e89-2c4b-4641-9a68-43b4cfc3f629", "9370c439-9ef0-40a3-a545-d10ff68bfa14", "9ed08237-e33f-4282-a9b4-b8c581a0c46e", "bd8b24c9-2202-44fc-9e1e-4b3e92acdbb4", "1412e573-5106-4bcb-aa05-6edbfb9c1040", "28fac61f-bdd7-4a52-bbb1-fc8596056648", "f57bdfe1-aeee-41c1-8ac6-e0af2a0bb848", "49eee110-1c95-4c59-bbda-6904b66b5acb", "cbbae368-03a4-4b3b-a5dc-be6f0eebd0f8", "dc80d246-57f1-43d0-abd5-5138678acfa7", "81bb4fe1-90b8-4a88-9473-0bff35a127c9", "d2b8ffd8-0c95-4aad-9786-a5ac10f07c40", "5f3597f4-740f-41c2-a307-b0353f195c7d", "a9169382-ad06-4477-8b6c-ac4b60c43003", "2d4840dd-2d18-4158-838a-47e3060a9e13", "04ebfc1c-56b0-48a7-92b6-faa02a7cefeb", "25efce68-88a7-4f41-a161-73a0e6f9d4ef", "544fec19-ef81-4dab-8c27-187d3df0a04a", "834e6d70-7a48-4065-ae1c-bc8f6c8fbc0e", "b9567c52-c635-410d-abc0-2f8a3b7d07a2", "50272c49-0640-491e-8645-2dcdacda5f53", "db816428-8204-4270-b077-fa7213e85514", "82d054fc-5a82-4abc-9f21-e5c62d8461da", "99c67645-2208-4185-9961-7517d4779222", "b073f1a9-bc07-4f83-81d4-520b036ad651", "b3d32c08-82c9-4fc8-a7f2-d46d89bd211a", "064d3055-45b2-4f92-b3f5-0ddeb24382a6", "53415340-08d7-4945-bd54-344a45f29a41", "3f258315-194a-4b2e-a031-9d5638e0fe4c", "b4ee2c5a-06b3-45b0-b0f1-31c94a1dae4c", "de893910-4994-438a-a02b-2db7bee5cdca", "ca120785-7c9b-43a4-974a-a98e5d0a9084", "77de8899-43c3-43d2-a2ff-dcb97439c7f8", "85309f25-a09b-4182-8152-ae6cde0a41ba", "c39f04d5-6107-4f3e-a637-2661d62910e7", "d065715d-14fb-4eeb-9e4c-867b41e7ea18", "103c790d-5d2d-4888-8a41-800cd97bf6ed", "e97c3bdb-5f6b-44a0-a1f9-a4be3f0b29cf", "7d6f7f58-ab20-4309-a7f0-9d39f76c6663", "913e38d9-562e-4a49-8089-3b5d0602be28", ...]

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New

We're in Beta

About us Mission Statement