fbutano

fbutano

Hello, I have a small phoenix app with just one ecto model called Office with the following schema for parent relationship

defmodule Tree.Arma.Office do
  use Ecto.Schema
  import Ecto.Changeset

  schema "offices" do
    field :name, :string
    belongs_to :parent, Tree.Arma.Office
    has_many :children, Tree.Arma.Office
    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(office, attrs) do
    office
    |> cast(attrs, [:id, :name, :parent_id])
    |> validate_required([:name])
  end
end

Inside Arma context I have the following helper function to retrieve a linear list of offices in parent-children relations and works perfectly

def list_descendants(id) do
    base_query =
      Office
      |> where(id: ^id)

    recursive_query =
      Office
      |> join(:inner, [next_descendant], parent in "descendants_list",
        on: next_descendant.parent_id == parent.id
      )

    descendants_query = base_query |> union_all(^recursive_query)

    {"descendants_list", Office}
    |> recursive_ctes(true)
    |> with_cte("descendants_list", as: ^descendants_query)
    |> where([o], o.id != ^id)
    |> Repo.all()
  end

I have also defined the following functions to retrieve a nested map of descendants:

  def nested_tree(parent_id) do
    flat_list = list_descendants(parent_id)
    nester(flat_list, parent_id)
  end

  def nester(flat_list, root_id) do
    lookup = Enum.group_by(flat_list, & &1.parent_id)
    with_children(root_id, lookup)
  end

  defp with_children(root_id, lookup) do
    lookup
    |> Map.get(root_id, [])
    |> Enum.map(&nest_one(&1, lookup))

  end

  defp nest_one(row, lookup) do
    %{row | children: with_children(row.id, lookup)}
  end

So, if we have an Office with id=4 and call nested_tree(4) the function returns a nested map of all the descendants from the Office with id=4.

The problem is that when i call nested_tree from inside an iex -S mix shell all works fine; but when i the function is called from the liveview that shows the details for a sibgle Office, nested_tree() return an empty list. This is the code of the liveview:

defmodule TreeWeb.OfficeLive.Show do
  use TreeWeb, :live_view

  alias Tree.Arma

  @impl true
  def mount(_params, _session, socket) do
    {:ok, socket}
  end

  @impl true
  def handle_params(%{"id" => id}, _, socket) do
    {:noreply,
     socket
     |> assign(:page_title, page_title(socket.assigns.live_action))
     |> assign(:office, Arma.get_office!(id))
     |> assign(:office_tree, Arma.nested_tree(id))
    }
  end

  defp page_title(:show), do: "Show Office"
  defp page_title(:edit), do: "Edit Office"
end

I’ve found that if in the liveview the id of the office is hardcoded when calling nested_tree() it works!
So for example if in the liveview I put:

|> assign(:office_tree, Arma.nested_tree(4)

it works!

I’m really stuck with this, so if anybody can help me I really appreciate!!

Showing Posts 1 to 4

Hermanverschooten

Hermanverschooten

What happens if you call your function with a quoted value, ”4”?

fbutano

fbutano OP

Hello and many thanks for your reply!

if I call nested_tree(“4”) returns an empty list

nTraum

nTraum

Then maybe passing the ID either as a string or as an integer is the reason for the different behaviour you observe.

The params map in the handle_params function always contains string values as far as I know. What happens if you parse to an integer before passing it to your functions?

@impl true
  def handle_params(%{"id" => unsigned_id}, _, socket) do
    id = String.to_integer(unsigned_id)

    {:noreply,
     socket
     |> assign(:page_title, page_title(socket.assigns.live_action))
     |> assign(:office, Arma.get_office!(id))
     |> assign(:office_tree, Arma.nested_tree(id))
    }
  end
fbutano

fbutano OP

It works! perfect explanation and solution to the problem!
Many thanks for your fast reply!

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews